简体   繁体   中英

Moment.js - time elapsed with local timezone and UNIX input

I am working with an API that returns dates in UNIX format: ie 1441647410 .

With the following moment.js helper, I am able to convert it to a more readable format ( 05/22/15, 09:33 ):

UI.registerHelper('formatUnix', function(context, options) {
  if(context)
    return moment.unix(context).format('MM/DD/YY, hh:mm');
});

Instead of displaying an absolute time, I'd like to display time elapsed from now (ie 5 hrs ago, or 11 weeks ago) in this example. How could I do that? I am trying to define now with moment() , but it doesn't seem to be working:

UI.registerHelper('fromNow', function(context, options) {
  if(context)
    var now = moment();
    var diff = now - context;
    return moment.unix(diff).format('hh');
});

How can I should an amount of time elapsed from now (11 weeks ago) instead of an absolute time (5/22/15)? Also, is there any way for the browser to automatically grab the client's local time?

Thanks !

将unix解析为瞬间时间然后使用from呢

return moment(context).from(moment());

亲密无间,在使用fromNow()构造了矩实例之后,只需要使用fromNow()方法 moment.unix()

moment.unix(context).fromNow()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM