问题是如何将JavaScript Date格式化为一个字符串,声明时间经过,类似于您在Stack Overflow上看到的时间显示方式。
e.g.
1分钟前 1小时前 1天前 1个月前 一年前
问题是如何将JavaScript Date格式化为一个字符串,声明时间经过,类似于您在Stack Overflow上看到的时间显示方式。
e.g.
1分钟前 1小时前 1天前 1个月前 一年前
当前回答
const timeHandler = (time: any) => {
var a = moment(); //now
var b = moment.utc(time);
const sec = a.diff(b, 'seconds');
const minuts = a.diff(b, 'minutes');
const hours = a.diff(b, 'hours');
const days = a.diff(b, 'days');
const weeks = a.diff(b, 'weeks');
return weeks == 0
? days == 0
? hours == 0
? minuts == 0
? sec >= 0 && `${sec}s ago`
: `${minuts}m ago`
: `${hours}h ago`
: `${days}d ago`
: `${weeks}w ago`;
};
console.log(timeHandler('2022-11-11T12:11:06.6103808'))
其他回答
您可能需要查看humanized_time_span: https://github.com/layam/js_humanized_time_span
它与框架无关,并且完全可定制。
只需下载/包含脚本,然后你可以这样做:
humanized_time_span("2011-05-11 12:00:00")
=> '3 hours ago'
humanized_time_span("2011-05-11 12:00:00", "2011-05-11 16:00:00)
=> '4 hours ago'
甚至是这样:
var custom_date_formats = {
past: [
{ ceiling: 60, text: "less than a minute ago" },
{ ceiling: 86400, text: "$hours hours, $minutes minutes and $seconds seconds ago" },
{ ceiling: null, text: "$years years ago" }
],
future: [
{ ceiling: 60, text: "in less than a minute" },
{ ceiling: 86400, text: "in $hours hours, $minutes minutes and $seconds seconds time" },
{ ceiling: null, text: "in $years years" }
]
}
humanized_time_span("2010/09/10 10:00:00", "2010/09/10 10:00:05", custom_date_formats)
=> "less than a minute ago"
更多信息请阅读文档。
我还没有检查(虽然这并不难),但我认为Stack Exchange站点使用jquery。Timeago插件来创建这些时间字符串。
这个插件使用起来很简单,而且很干净,还能自动更新。
下面是一个简单的例子(来自插件的主页):
First, load jQuery and the plugin: <script src="jquery.min.js" type="text/javascript"></script> <script src="jquery.timeago.js" type="text/javascript"></script> Now, let's attach it to your timestamps on DOM ready: jQuery(document).ready(function() { jQuery("abbr.timeago").timeago(); }); This will turn all abbr elements with a class of timeago and an ISO 8601 timestamp in the title: <abbr class="timeago" title="2008-07-17T09:24:17Z">July 17, 2008</abbr> into something like this: <abbr class="timeago" title="July 17, 2008">about a year ago</abbr> which yields: about a year ago. As time passes, the timestamps will automatically update.
这是一个简化版的@sky-sanders的回答。
function timeSince(date) {
var seconds = Math.floor((new Date() - date) / 1000);
var divisors = [31536000, 2592000, 86400, 3600, 60, 1]
var description = ["years", "months", "days", "hours", "minutes", "seconds"]
var result = [];
var interval = seconds;
for (i = 0; i < divisors.length; i++) {
interval = Math.floor(seconds / divisors[i])
if (interval > 1) {
result.push(interval + " " + description[i])
}
seconds -= interval * divisors[i]
}
return result.join(" ")
}
虽然这个问题是很久以前问过的,但写下这个答案希望它能帮助到一些人。
传递您想要开始计数的日期。使用momentjs的moment().fromNow():(在这里查看更多信息)
getRelativeTime(date) {
const d = new Date(date * 1000);
return moment(d).fromNow();
}
如果您想更改日期fromNow提供的信息,请编写自定义相对时间为时刻。
例如,在我自己的例子中,我想打印'one month ago'而不是'a month ago'(由moment(d). fromnow()提供)。在这种情况下,你可以写出下面给出的内容。
moment.updateLocale('en', {
relativeTime: {
future: 'in %s',
past: '%s ago',
s: 'a few seconds',
ss: '%d seconds',
m: '1 m',
mm: '%d minutes',
h: '1 h',
hh: '%d hours',
d: '1 d',
dd: '%d days',
M: '1 month',
MM: '%d months',
y: '1 y',
yy: '%d years'
}
});
注意:我是在Angular 6中为项目编写代码的
将上面的函数更改为
function timeSince(date) {
var seconds = Math.floor(((new Date().getTime()/1000) - date)),
interval = Math.floor(seconds / 31536000);
if (interval > 1) return interval + "y";
interval = Math.floor(seconds / 2592000);
if (interval > 1) return interval + "m";
interval = Math.floor(seconds / 86400);
if (interval >= 1) return interval + "d";
interval = Math.floor(seconds / 3600);
if (interval >= 1) return interval + "h";
interval = Math.floor(seconds / 60);
if (interval > 1) return interval + "m ";
return Math.floor(seconds) + "s";
}
否则它会显示“75分钟”(介于1到2小时之间)。它现在还假定输入日期是Unix时间戳。