javascript 需要使用 MomentJS 显示 AM/PM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31924811/
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-28 14:35:18  来源:igfitidea点击:

Need to show AM/PM with MomentJS

javascriptdatetimemomentjs

提问by ChristyPiffat

I am trying to format some dates using MomentJS. I have no problem until I try to add AM/PM or am/pm. I have the following function and am passing in the time from the results of a Breeze EntityQuery where the time is a System.DateTime as shown:

我正在尝试使用 MomentJS 格式化一些日期。在我尝试添加 AM/PM 或 am/pm 之前,我没有问题。我有以下函数,并从 Breeze EntityQuery 的结果中传入时间,其中时间是 System.DateTime,如图所示:

function datetimeCellRendererFunc(value) {
    // value = Mon Jun 15 2015 09:00:00 GMT-0500 (Central Daylight Time);
    return moment(value).format("MM/DD/YYYY h:mm A");
}

Whether I use A or a in the formatting, I still end up with the following:

无论我在格式中使用 A 还是 a ,我仍然得到以下结果:

06/15/2015 9:00 上午

06/15/2015 9:00 上午

Is there something else I need to add? Thank you in advance!!

还有什么我需要补充的吗?先感谢您!!

采纳答案by Jonathan Chan

To force the English locale globally, add

要在全球范围内强制使用英语语言环境,请添加

moment.locale('en');

to your code.

到您的代码。

To configure it for a specific momentinstance, you could also use

要为特定moment实例配置它,您还可以使用

moment(value).locale('en').format(/* ... */);

in your function.

在你的函数中。