0

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)

2 Answers 2

8

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>

Sign up to request clarification or add additional context in comments.

Comments

1

Simply use the Format method

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.