Javascript 使用 moment js 将 12 小时 (AM/PM) 字符串转换为 24 日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26783080/
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
Convert 12 hour (AM/PM) string to 24 Date object using moment js
提问by ram
I have a output resulting from a timepicker giving 12 hour format of time.
我有一个时间选择器产生的输出,给出了 12 小时的时间格式。
Eg : "1:45 AM (or) "12:15 PM" as **string**
Is there a way to parse this string format to 24 hour using moment js back to date object?
有没有办法使用moment js返回日期对象将此字符串格式解析为24小时?
回答by Pochen
回答by Hakan F?st?k
I know that this answer is not for the question (actually it is for the opposite case), but I will put it for completeness and if someone (like me) was looking for it.
In case you want to convert from the 24 Hour systemto 12 Hour systemthen you could use the following
我知道这个答案不是针对这个问题的(实际上是针对相反的情况),但我会说它的完整性,如果有人(像我一样)正在寻找它。
如果您想从 to 转换24 Hour system,12 Hour system则可以使用以下内容
return moment("13", ["HH"]).format("hh A");
the previous code will produce the result 1 PM
前面的代码将产生结果 1 PM
回答by Neel Rathod
Just a little conversation "2 PM" to "14.00"
只是“下午 2 点”到“14.00”的小对话
const number = moment("02:00 PM", ["h:mm A"]).format("HH:mm");
cosole.log(number); // "14.00"
"14.00" to "2 PM"
“14.00”至“下午 2 点”
const number = moment("14.00", ["HH.mm"]).format("hh:mm a");
cosole.log(number); // "02:00 pm"
回答by Ravindra Vairagi
In full calender to show time in 12 hour format like 01:00 PM i used following format - timeFormat: 'hh:mm A'. "timeFormat" is the property of events of fullcalender.
在完整的日历中以 12 小时格式显示时间,例如 01:00 PM,我使用了以下格式 - timeFormat: 'hh:mm A'。“timeFormat”是fullcalender事件的属性。
Same format 'hh:mm A' we can use to format datetime object using momentjs.
我们可以使用相同的格式 'hh:mm A' 使用 momentjs 来格式化日期时间对象。
回答by Prashanti.D
moment("145","hmm").format("HH:mm");
this would result in 01:45
这将导致 01:45
回答by Priyanka Shirsath
var today = new Date();
let options = {
hour: "2-digit", minute: "2-digit"
};
console.log(today.toLocaleTimeString("en-us", options));
回答by Jong Yun Kim
using HH:mm will convert 12 hrs format to 24hrs while using hh:mm will convert 12hrs format
使用 HH:mm 会将 12 小时格式转换为 24 小时,而使用 hh:mm 将转换 12 小时格式
moment("12:15 PM").format("HH:mm")

