javascript Moment.js - 如何从日期和时间字符串构建时刻?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16808911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:04:08  来源:igfitidea点击:

Moment.js - how do I build moment from date and time string?

javascriptmomentjs

提问by Amitava

If I have given date string MM-DD-YYYYand time string HH:mm Aor H:m a, how do I build a momentobject?

如果我给出了日期字符串MM-DD-YYYY和时间字符串HH:mm Aor H:m a,我该如何构建一个时刻对象?

I tried moment('MM-DD-YYYY HH:mm A')which did not work.

我试过moment('MM-DD-YYYY HH:mm A')哪个不起作用。

回答by Michi

Try using the format (second) parameter moment("12-25-1995", "MM-DD-YYYY");

尝试使用格式(第二个)参数 moment("12-25-1995", "MM-DD-YYYY");

For details see http://momentjs.com/docs/#/parsing/string-format/

有关详细信息,请参阅http://momentjs.com/docs/#/parsing/string-format/

回答by Suman Kundu

To create a moment objectof your desired time, do the following:

要创建object您想要的时刻,请执行以下操作:

moment('05-17-2018 11:40 PM', 'MM-DD-YYYY hh:mm A')

In this case '05-17-2018 11:40 PM' is the desired time.

在这种情况下,'05-17-2018 11:40 PM' 是所需的时间。