简体   繁体   English

使用moment.js格式化日期

[英]Format date using moment.js

I'm using the moment.js library to format dates in the format MM DD, YYYY . 我正在使用moment.js库将日期格式设置为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() : 无需使用JavaScript Date来解析您的字符串,只需使用moment(string) (因为您的输入为ISO 8001格式),则必须使用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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM