Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
76
rated 0 times [  80] [ 4]  / answers: 1 / hits: 133519  / 10 Years ago, mon, may 12, 2014, 12:00:00

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 trivial as this:



var beginningTime = moment('8:45am');
var endTime = moment('9:00am');
console.log(beginningTime.isBefore(endTime)); //false???


Fiddle: http://jsfiddle.net/KyleMuir/M4R4z/



Is there something we are missing? It feels like this shouldn't be a hard problem to solve. When we perform any moment functions on our beginningTime or endTime it simply says NAN


More From » momentjs

 Answers
2

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

<script src=https://cdn.jsdelivr.net/momentjs/2.13.0/moment.min.js></script>





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



JSFiddle


[#71051] Sunday, May 11, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jackelyn

Total Points: 303
Total Questions: 103
Total Answers: 102

Location: Turks and Caicos Islands
Member since Sun, Mar 7, 2021
3 Years ago
jackelyn questions
Thu, Apr 8, 21, 00:00, 3 Years ago
Sun, Feb 28, 21, 00:00, 3 Years ago
Mon, May 25, 20, 00:00, 4 Years ago
Thu, Apr 30, 20, 00:00, 4 Years ago
;