简体   繁体   English

使用 Javascript 将 24 小时制 JSON 数据转换为 12 小时制

[英]Converting JSON data 24 hour time to 12 hour with Javascript

The JSON I'm pulling returns the time and date information together in this format:我正在提取的 JSON 以这种格式一起返回时间和日期信息:

06-16-2013 16:00:00

What I need to do is convert it from 24 hour time to 12 hour time (with AM and PM, preferably).我需要做的是将其从 24 小时制转换为 12 小时制(最好使用 AM 和 PM)。 I've seen similar posts and functions on here, but none that seem to work with this particular formatting ( here is one good example ).我在这里看到过类似的帖子和功能,但似乎没有一个适用于这种特定格式( 这是一个很好的例子)。

My JavaScript isn't superb, which might be part of the problem when it comes to getting other functions to work for me.我的 JavaScript 不是很好,这可能是让其他函数为我工作时出现问题的一部分。

You can use momentjs to format the date in the way you prefer.您可以使用momentjs以您喜欢的方式格式化日期。 First include momentjs http://momentjs.com/ in your project, and then you will be able to do:首先在您的项目中包含 momentjs http://momentjs.com/ ,然后您将能够执行以下操作:

moment('06-16-2013 16:00:00','MM-DD-YYYY HH:MM:SS').format('MM-DD-YYYY hh:mm:ss a')

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

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