简体   繁体   English

JS返回错误的日期

[英]moment JS returns wrong date

I'm passing UTC timestamp to moment js and timezone in order to get back the real date. 我正在将UTC时间戳传递给时刻js和时区,以获取真实日期。

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'); 因此,我将传递诸如formatDate(1399922165, 'America/Los_Angeles'); and it returns 12-Jan-9992 instead of 12-May-2014. 并返回2014年1月12日而不是2014年5月12日。

If instead I do it like this: 如果相反,我这样做是这样的:

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

Then it returns 16-Jan-1970. 然后返回1970年1月16日。

Thanks to Ian, this ended up being the solution. 多亏了Ian,这才成为解决方案。

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

Any ideas? 有任何想法吗?

Thanks to Ian, this ended up being the solution. 多亏了Ian,这才成为解决方案。

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

I was trying moment.utc() instead of moment.unix() . 我正在尝试moment.utc()而不是moment.unix()

The strange results came from moment.utc(datetime, format) expecting datetime to match format . 奇怪的结果来自moment.utc(datetime, format)期望datetimeformat匹配。 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. 但是,请务必注意, moment.utc(datetime)仍会返回1970年的结果,因此即使没有格式也不会返回期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM