简体   繁体   中英

How to parse given date string using moment.js?

I want the following date string to be formatted using moment.js but it gives invalid date.

var dateString = '2/17/2016 12:16PM GMT-05:00';
var pattern = 'MM/DD/YYYY h:mma ZZ';
var testDate = moment(dateString).format(pattern); //Invalid Date

How to parse it correctly or is there any other way to do so?

Just use pattern as second parameter in moment function

var testDate = moment(dateString, pattern)

more here in the docs: http://momentjs.com/docs/#/parsing/string-format/

You can try it:

 var dateString = '2/17/2016 12:16PM GMT-05:00'; var pattern = 'MM/DD/YYYY h:mma ZZ'; var testDate = moment(dateString, "MM/DD/YYYY h:mmA -hh:mm").format(pattern);

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