Question

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?

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top