Skip to content
Advertisement

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?

Advertisement

Answer

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/

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement