简体   繁体   中英

Moment.js How to convert MM/dd/yyyy HH:mm:ss.fff to round-trip date/time pattern?

如何通过使用标准JavaScript或moment.js库将字符串"8/3/2017 6:12:00 AM""2017-08-03T06:12:00.000"

您可以通过以下方式使用矩将8/3/2017 6:12:00 AM转换为2017-08-03T06:12:00.000

moment("8/3/2017 6:12:00 AM", 'DD/MM/YYYY HH:mm:ss a').format('YYYY-DD-MMTHH:mm:ss.000')

 console.log(moment("8/3/2017 6:12:00 AM", 'DD/MM/YYYY HH:mm:ss a').format('YYYY-DD-MMTHH:mm:ss.000')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js"></script> 

if you know what format you are getting then i suggest to parse and create an js date object. and then

 var year = 2017; var month = 2; var day = 8; var date = new Date(year, month, day); console.log(date.toISOString()); 

Updated:

 console.log(moment("8/3/2017 6:12:00 AM", 'DD/MM/YYYY HH:mm:ss a').format('YYYY-DD-MMTHH:mm:ss.000')) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.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