how to get current time and then compare them using jquery

18,081

If you really want to know whether two Date objects represent precisely the same time, or are before/after one another, it's quite easy: just compare the two Dates via the getTime() method, which returns an integer timestamp for the object. For example,

var date1 = myDate,
    date2 = new Date();
return (date1.getTime() < date2.getTime());

would return true if myDate is before 'now', false if it is now or in the future.

Share:
18,081
zjm1126
Author by

zjm1126

Every morning when I opened my eyes ,the responsibility of deliver the earth falls on my shoulders.

Updated on June 14, 2022

Comments

  • zjm1126
    zjm1126 almost 2 years

    this is my code:

        var currentTime = new Date()
    var month = currentTime.getMonth() + 1
    var day = currentTime.getDate()
    var year = currentTime.getFullYear()
    var hours = currentTime.getHours()
    var minutes = currentTime.getMinutes()
    
    alert(minutes+"/"+hours+"/"+month + "/" + day + "/" + year)
    

    but , i think it hard to compare with two time ,

    what can i do ?

    thanks

    • Darin Dimitrov
      Darin Dimitrov over 13 years
      You usually need two quantities in order to perform a comparison. So you want to compare the current time to what?
  • devmonster
    devmonster over 11 years
    what is mydate? how do I creat it from the string "16:30" or "4:30PM"