0

I would like to know how to compare two time in javascript, time is in 24 hours format.

function checkTime (stime,etime) {
  if(stime<etime){
   return true
  }
  else{
   return false
  }
}

var r1 = this.checkTime("10:20", "21:30"); // true
var r2 = this.checkTime("23:20", "19:30"); //false
codecat
  • 145
  • 1
  • 11
  • Probably the best JS library out there to handle dates is [Moment.js](https://momentjs.com/) – yunzen Aug 28 '20 at 06:52
  • Does this answer your question? [How can I compare two time strings in the format HH:MM:SS?](https://stackoverflow.com/questions/6212305/how-can-i-compare-two-time-strings-in-the-format-hhmmss) – briosheje Aug 28 '20 at 06:53

1 Answers1

0

Here simplest way:

function checkTime (stime,etime) {
            return Date.parse('01/01/1000 ' + stime) < Date.parse('01/01/1000 ' + etime)
        }

        var r1 = checkTime("10:20", "21:30"); // true
        var r2 = checkTime("23:20", "19:30"); //false

dd/mm/yy not important so just compare hh:mm

Arman
  • 481
  • 4
  • 12