问题是如何将JavaScript Date格式化为一个字符串,声明时间经过,类似于您在Stack Overflow上看到的时间显示方式。
e.g.
1分钟前 1小时前 1天前 1个月前 一年前
问题是如何将JavaScript Date格式化为一个字符串,声明时间经过,类似于您在Stack Overflow上看到的时间显示方式。
e.g.
1分钟前 1小时前 1天前 1个月前 一年前
当前回答
我还没有检查(虽然这并不难),但我认为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.
其他回答
我还没有检查(虽然这并不难),但我认为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.
function calDateAgo(dString=null){
//var dString = "2021-04-1 12:00:00";
var d1 = new Date(dString);
var d2 = new Date();
var t2 = d2.getTime();
var t1 = d1.getTime();
var d1Y = d1.getFullYear();
var d2Y = d2.getFullYear();
var d1M = d1.getMonth();
var d2M = d2.getMonth();
var time_obj = {};
time_obj.year = d2.getFullYear()-d1.getFullYear();
time_obj.month = (d2M+12*d2Y)-(d1M+12*d1Y);
time_obj.week = parseInt((t2-t1)/(24*3600*1000*7));
time_obj.day = parseInt((t2-t1)/(24*3600*1000));
time_obj.hour = parseInt((t2-t1)/(3600*1000));
time_obj.minute = parseInt((t2-t1)/(60*1000));
time_obj.second = parseInt((t2-t1)/(1000));
for (const obj_key in time_obj) {
if(time_obj[obj_key] == 0){
delete time_obj[obj_key];
}
}
var ago_text = 'just now';
if(typeof Object.keys(time_obj)[0] != 'undefined'){
var time_key = Object.keys(time_obj)[0];
var time_val = time_obj[Object.keys(time_obj)[0]];
time_key += (time_val > 1) ? 's':'';
ago_text = time_val+' '+time_key+' ago';
}
return ago_text;
}
虽然这个问题是很久以前问过的,但写下这个答案希望它能帮助到一些人。
传递您想要开始计数的日期。使用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中为项目编写代码的
您可能需要查看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"
更多信息请阅读文档。
我一直在寻找这个问题的答案,并且几乎实现了其中一个解决方案,但一位同事提醒我检查react-intl库,因为我们已经在使用它了。
所以在解决方案中…在使用react-intl库的情况下,它们有一个<FormattedRelative>组件。
https://github.com/yahoo/react-intl/wiki/Components#formattedrelative