Moment.js - 使用本地时区和 UNIX 输入的经过时间
Moment.js - time elapsed with local timezone and UNIX input
我正在使用 API,returns 日期为 UNIX 格式:即 1441647410.
使用以下 moment.js 帮助程序,我可以将其转换为更具可读性的格式(05/22/15,09:33):
UI.registerHelper('formatUnix', function(context, options) {
if(context)
return moment.unix(context).format('MM/DD/YY, hh:mm');
});
在此示例中,我不想显示绝对时间,而是显示从现在开始经过的时间(即 5 小时前或 11 周前)。我怎么能那样做?我正在尝试使用 moment()
进行定义,但它似乎不起作用:
UI.registerHelper('fromNow', function(context, options) {
if(context)
var now = moment();
var diff = now - context;
return moment.unix(diff).format('hh');
});
我怎样才能得到从现在开始(11 周前)过去的时间而不是绝对时间(5/22/15)?还有,有没有办法让浏览器自动抓取客户端的本地时间?
谢谢!
把unix解析成moment time然后用from:
怎么样
return moment(context).from(moment());
你很接近,你只需要在使用 moment.unix()
:
构造时刻实例后使用 fromNow()
method
moment.unix(context).fromNow()
我正在使用 API,returns 日期为 UNIX 格式:即 1441647410.
使用以下 moment.js 帮助程序,我可以将其转换为更具可读性的格式(05/22/15,09:33):
UI.registerHelper('formatUnix', function(context, options) {
if(context)
return moment.unix(context).format('MM/DD/YY, hh:mm');
});
在此示例中,我不想显示绝对时间,而是显示从现在开始经过的时间(即 5 小时前或 11 周前)。我怎么能那样做?我正在尝试使用 moment()
进行定义,但它似乎不起作用:
UI.registerHelper('fromNow', function(context, options) {
if(context)
var now = moment();
var diff = now - context;
return moment.unix(diff).format('hh');
});
我怎样才能得到从现在开始(11 周前)过去的时间而不是绝对时间(5/22/15)?还有,有没有办法让浏览器自动抓取客户端的本地时间?
谢谢!
把unix解析成moment time然后用from:
怎么样return moment(context).from(moment());
你很接近,你只需要在使用 moment.unix()
:
fromNow()
method
moment.unix(context).fromNow()