简体   繁体   中英

Invalid date in Javascript using MomentJS

MomentJS keeps printing invalid date for the following value: Time: '2018-01-01T00:01:01.001+01:00' , which is part of Object loraContent .

I am currently calling it like this: var formatted = moment(loraContent.Time, "yyyy-mm-ddThh:mm:ss.sss+01:00").format("hh:mm A");

When logging formatted the console returns "Invalid Date"

You should use uppercase letters for Year, month, and day tokens ( docs ):

 let time = '2018-01-01T00:01:01.001+01:00' let formatted = moment(time, "YYYY-MM-DDThh:mm:ss.sss+01:00").format("hh:mm A"); console.log(formatted)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

Seems like v2.5.1 keeps some compatibility with lowercase.

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