简体   繁体   English

如何使用 moment.js 解析给定的日期字符串?

[英]How to parse given date string using moment.js?

I want the following date string to be formatted using moment.js but it gives invalid date.我希望使用 moment.js 格式化以下日期字符串,但它给出的日期无效。

var dateString = '2/17/2016 12:16PM GMT-05:00';
var pattern = 'MM/DD/YYYY h:mma ZZ';
var testDate = moment(dateString).format(pattern); //Invalid Date

How to parse it correctly or is there any other way to do so?如何正确解析它或有其他方法吗?

Just use pattern as second parameter in moment function只需在矩函数中使用模式作为第二个参数

var testDate = moment(dateString, pattern)

more here in the docs: http://momentjs.com/docs/#/parsing/string-format/文档中的更多内容:http: //momentjs.com/docs/#/parsing/string-format/

You can try it:你可以试试:

 var dateString = '2/17/2016 12:16PM GMT-05:00'; var pattern = 'MM/DD/YYYY h:mma ZZ'; var testDate = moment(dateString, "MM/DD/YYYY h:mmA -hh:mm").format(pattern);

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

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