Jeyabalan Thavamani
Jeyabalan Thavamani

Reputation: 3327

Can't format the date using moment.js

Can't format the below date using moment.js, the below statement returns Invalid Date

moment('20171206T062406927Z').format('D-MMM-YYYY');

Please help me on this.

Upvotes: 0

Views: 145

Answers (1)

searlea
searlea

Reputation: 8378

You need to tell moment which format your date string is in:

moment('20171206T062406927Z', 'YYYYMMDD[T]HHmmssSSSZ', true).format('D-MMM-YYYY');

Edit: updated as per @VincenzoC comment to ensure the timestamp is parsed in UTC

Also fix: use HH for 24-hour format (not hh), and pass a third true parameter to ensure the timestamp is parsed in strict mode.

Upvotes: 1

Related Questions