简体   繁体   中英

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.

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'
  • 24-hour format: '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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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