gpt4 book ai didi

javascript - 尝试从 JS 中的时间戳获取 1 天或 2 分钟前

转载 作者:行者123 更新时间:2023-12-02 18:43:39 24 4
gpt4 key购买 nike

嗨,我想自动将时间戳转换为类似的内容:

X 分钟前X 小时前X个月前

但我不知道之后要做什么:

function getTimeAgo(timestamp){
var a = new Date(timestamp*1000);
..
}

我是 JavaScript 新手,有人已经编写过这种函数吗?

谢谢

最佳答案

TimeAgo.js

What?

Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago"). Download, view the examples, and enjoy.

You opened this page less than a minute ago. (This will update every minute. Wait for it.)

This page was last modified about a month ago.

Ryan was born 34 years ago.

Usage:

jQuery.timeago(new Date());             //=> "less than a minute ago"
jQuery.timeago("2008-07-17"); //=> "5 years ago"
jQuery.timeago(jQuery("abbr#some_id")); //=> "5 years ago" // [title="2008-07-20"]

关于javascript - 尝试从 JS 中的时间戳获取 1 天或 2 分钟前,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com