简体   繁体   中英

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)

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> 

只需使用Format方法

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

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