最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - moment fromNow returns in 5 hours when parsing utc - Stack Overflow

programmeradmin0浏览0评论

trying to format utc time from server in time ago using moment.js fromNow but in some occasions I get "in 5 hours" instead.

timestamp from a server - 2017-11-29T15:03:21

var utcTime = new Date(timestamp);
var timeAgo = moment(utcTime).fromNow();    
console.log(timeAgo)

all dates are in past so how can I fix this so I dont get time in a few hours ?

trying to format utc time from server in time ago using moment.js fromNow but in some occasions I get "in 5 hours" instead.

timestamp from a server - 2017-11-29T15:03:21

var utcTime = new Date(timestamp);
var timeAgo = moment(utcTime).fromNow();    
console.log(timeAgo)

all dates are in past so how can I fix this so I dont get time in a few hours ?

Share Improve this question edited Dec 2, 2017 at 14:44 user1751287 asked Nov 29, 2017 at 22:27 user1751287user1751287 50111 silver badges26 bronze badges 3
  • Well what do you want? fromNow is returning exactly what the documentation states it should return. I am not sure what the confusion is. Maybe you are looking for difference? – Igor Commented Nov 29, 2017 at 22:31
  • I want to get a few seconds ago, few minutes ago...while I get in 5 hours which is in future. so I was wondering how to format it for timezones – user1751287 Commented Nov 29, 2017 at 22:33
  • 1 Note that "2017-11-29T15:03:21" will be parsed as local, so if the client timezone has a different offset to the server, it will represent a different moment in time. If you want it treated as UTC, append a "Z" to the timestamp: "2017-11-29T15:03:21Z". You might also do moment(timestamp + 'Z').fromNow(). – RobG Commented Nov 30, 2017 at 0:10
Add a comment  | 

3 Answers 3

Reset to default 14

If you want "2017-11-29T15:03:21" treated as UTC, you can either use moment's utc method or just append a "Z" to the string. Since you're already using moment.js, it's more reliable to parse it with moment.js than the built-in parser:

var timestamp = "2017-11-30T00:20:48";

// Append Z
console.log(moment(timestamp + 'Z').fromNow());

// Use .utc
console.log(moment.utc(timestamp).fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>

You need to tell moment that this date is in UTC using moment.utc

var utcTime = new Date(timestamp);
var timeAgo = moment.utc(utcTime).fromNow();

If you don't, moment assumes this date is in your local timezone (which I can tell is Eastern Standard Time by the offset).

In your local timezone, this date is actually 5 hours in the future. Only in UTC is it a few seconds ago, because your local timezone is 5 hours behind UTC.

As per documents https://momentjs.com/docs/#/displaying/fromnow/

you can customize the locale https://momentjs.com/docs/#/customization/relative-time/

As default locale future time will be future: "in %s", having in which is as per documents. if you want to change it then update the locale and use as you want.

Hope this helps

发布评论

评论列表(0)

  1. 暂无评论