简体   繁体   English

Moment.js接受12小时和24小时时间

[英]Moment.js Accept both 12-hour and 24-hour time

Is there a way in Moment.js to accept both 12-hour and 24-hour times in an input filed. Moment.js中是否有一种方法可以接受输入字段中的12小时和24小时时间。

For example 例如

var myField= $('#myField').val();
var validTime = moment(myField, [h:mm A]).format(HHmm);

I would like to accept: 我想接受:

  • 12-hour format: '2:35 pm' 12小时格式:“下午2:35”
  • 24-hour format: '14:35' 24小时格式:“ 14:35”

I can't find any documentation on parsing multiple formats. 我找不到解析多种格式的任何文档。 Perhaps I should test the value using a regex? 也许我应该使用正则表达式测试值?

I think you simply use an array of formats. 我认为您只是使用多种格式。

https://momentjs.com/docs/#/parsing/string-formats/ https://momentjs.com/docs/#/parsing/string-formats/

var validTime = moment(myField, ["h:mm A", "H:mm"]);

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

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