Moment Fromnow Returns In 5 Hours When Parsing Utc
Solution 1:
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 Zconsole.log(moment(timestamp + 'Z').fromNow());
// Use .utcconsole.log(moment.utc(timestamp).fromNow());
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment.min.js"></script>
Solution 2:
You need to tell moment that this date is in UTC using moment.utc
var utcTime = newDate(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.
Solution 3:
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
Post a Comment for "Moment Fromnow Returns In 5 Hours When Parsing Utc"