Not able to convert Date String to required format using MomentJS

时间:2018-12-03 12:52:51

标签: momentjs

I am trying to convert a Date String of format

Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time)

into a format of YYYY-MM-DD so as to use it for a value in input date but it's not able to parse it.

I tried using these formats but it didn't work.

moment(new Date(Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time))).format("YYYY-MM-DD")

But since it's not a Date.parse() format, it doesn't work.

Any ideas would be welcome.

1 个答案:

答案 0 :(得分:1)

您需要在创建矩对象时传递dateFormat,以便其识别和解析非默认格式。

尝试

var dateFormat = "ddd MMM DD YYYY HH:mm:ss zZZ";
var m = moment("Mon Dec 03 2018 05:30:00 GMT+0530 (India Standard Time)", dateFormat);

m.format("YYYY-MM-DD");

有关从字符串解析的更多详细信息:https://momentjs.com/docs/#/parsing/string-format/