The Correct Way To Compare Time In Javascript?
I'm trying to compare time a and time b. My JavaScript looks like this var a ='02/08/2016 9:00 AM'; var b ='02/08/2016 11:20 PM'; if (b
Solution 1:
You have to turn them into dates and compare the time. getTime
is not needed if you are sure you are going to compare using < <= >= >
operators but won't work with equality == === != !==
operators so I think it's best to just go for getTime
This is a simple and quick way to do a comparison but i suggest you to check how dates work (also what's the best format for dates)
var a =newDate("02/08/2016 9:00 AM");
var b =newDate("02/08/2016 11:20 PM");
if (b.getTime()<=a.getTime()){
alert("End time is before or same as Start time.");
}
else{
alert("Start time is before End time.")
}
Post a Comment for "The Correct Way To Compare Time In Javascript?"