lomse
lomse

Reputation: 4165

Format date using moment.js

I'm using the moment.js library to format dates in the format MM DD, YYYY. What am I missing?

Code below:

import moment from 'moment';

var dateStr = '2015-10-19T13:33:52.140Z';

var d = new Date(dateStr); // Mon Oct 19 2015 13:33:52 GMT+0000 (GMT)

console.log( moment(d, "MM DD, YYYY").toDate() ) // Still outputs Mon Oct 19 2015 13:33:52 GMT+0000 (GMT)

Upvotes: 0

Views: 7763

Answers (2)

iLearn
iLearn

Reputation: 1179

Simply use the Format method

moment(dateStr).format("MM DD, YYYY");

Upvotes: 1

VincenzoC
VincenzoC

Reputation: 31482

There is no need tou use JavaScript Date to parse your string, just use moment(string) (because your input is in ISO 8001 format), then you have to use format():

var dateStr = '2015-10-19T13:33:52.140Z';
console.log( moment(dateStr).format("MM DD, YYYY"))
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

Upvotes: 8

Related Questions