문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top