Comparing two times with Moment JS

后端 未结 4 925
[愿得一人]
[愿得一人] 2020-11-28 09:11

I have a problem that requires me to take two times in 12 hour format and compare them, we have moment.js included in our project and we initially thought it would be as tri

4条回答
  •  旧时难觅i
    2020-11-28 09:57

    If you are always dealing with the time in h:mma format, you can specify it when parsing...

    var beginningTime = moment('8:45am', 'h:mma');
    var endTime = moment('9:00am', 'h:mma');
    console.log(beginningTime.isBefore(endTime)); // true
    console.log(beginningTime.toDate()); // Mon May 12 2014 08:45:00
    console.log(endTime.toDate()); // Mon May 12 2014 09:00:00

    It will use today as the date, so it won't work if you are spanning different days.

    JSFiddle

提交回复
热议问题