简体   繁体   中英

moment JS returns wrong date

I'm passing UTC timestamp to moment js and timezone in order to get back the real date.

This is how I'm doing it:

formatDate: function(dateTime, timezone) {
    var format = 'D-MMM-YYYY';
    return moment.utc(dateTime, format).tz(timezone).format(format);
}

So I would pass on something like formatDate(1399922165, 'America/Los_Angeles'); and it returns 12-Jan-9992 instead of 12-May-2014.

If instead I do it like this:

moment(dateTime).tz(timezone).format(format);

Then it returns 16-Jan-1970.

Thanks to Ian, this ended up being the solution.

moment.unix(dateTime).tz(timezone).format(format);

Any ideas?

Thanks to Ian, this ended up being the solution.

moment.unix(dateTime).tz(timezone).format(format);

I was trying moment.utc() instead of moment.unix() .

The strange results came from moment.utc(datetime, format) expecting datetime to match format . However, it's important to note that moment.utc(datetime) still returns 1970 year result so it still wouldn't have returned the desired result even without the format.

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