简体   繁体   中英

Moment Timezone - Invalid Date

In the snippet below, parsing moment.tz('30 3 14:59', 'wd HH:mm', 'America/Los_Angeles'); works fine but changing 14:59 to anything between 15:00 and 23:59 gives an Invalid date .

var validTime = moment.tz('30 3 14:59', 'w d HH:mm', 'America/Los_Angeles');
console.log(validTime.format()); // 2018-07-25T14:59:00-07:00


var invalidTime = moment.tz('30 3 15:00', 'w d HH:mm', 'America/Los_Angeles');
console.log(invalidTime.format()); // Invalid date

https://jsfiddle.net/084e2k91/26/

What's the issue here?

That's really interesting because I am also having the same problem, I think this is a bug in the moment.js which is not converting the date from other time zones. But when you try with do then everything seems fine. So my suggestion would be let use do for time being while time we raise this issue in GitHub.

And do is the same as d can be checked here in the docs . It means both return same day but one as a number other as a string .

With more experiment I found in IST after 18:30 the problem is starting. But the cause is really not understandable to me. But maybe cause is the addition of timezone value to the real-time which is causing the error. Mean if I am in IST then 00:00 - 5:30 is the same time from when the error occurs.

So updated code should be like following:

 var myTime1 = moment.tz('30 3 14:59', 'wd HH:mm', 'America/Los_Angeles'); console.log(myTime1.format()); var myTime = moment.tz('30 3rd 14:59', 'w do HH:mm', 'America/Los_Angeles'); console.log(myTime.format()); //In IST the problem is starting from 18:30 var myTime1 = moment.tz('30 3 19:30', 'wd HH:mm', 'America/Los_Angeles'); console.log(myTime1.format()); var myTime1 = moment.tz('30 3rd 19:30', 'w do HH:mm', 'America/Los_Angeles'); console.log(myTime1.format()); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.21/moment-timezone-with-data-2012-2022.min.js"></script> 

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