Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare time in javascript?

I have two time in the format "HH:MM" i want to compare them i have the following code to get the time of now in my format:

current_time = new Date();
hour = current_time.getHours();
minute = current_time.getMinutes();
if(hour<10){hour='0'+hour} if(minute<10){minute='0'+minute}
my_time = hour+':'+minute;

And this code is to get the time after subtracting the GMT difference :

d = new Date()
var n = d.getTimezoneOffset();
var n1 = Math.abs(n);
var difference = (n1/60); 
my_time = my_time - (0+difference);

Now the value of my_time should be compared with the value of match_time:

match_time = 10:00;//for example
if(my_time > match_time)
{
  alert('yes');
}
else
{
  alert('No');
}

how can I compare those values as time when they are a string ???

like image 517
Basel Avatar asked Sep 25 '13 12:09

Basel


People also ask

Can we compare time in JavaScript?

In JavaScript, we can compare two dates by converting them into numeric values to correspond to their time. First, we can convert the Date into a numeric value by using the getTime() function. By converting the given dates into numeric values we can directly compare them.

How do you compare two time strings?

Use string comparison to compare two time strings, that are formatted as hh:mm:ss , e.g. if (time1 > time2) {} . The the time strings are formatted as hh:mm:ss and are based on a 24 hour clock, the default behavior for string comparison is sufficient.

How does HH MM SS compare to Java?

If the time is formatted as HH:mm:ss, this should do the trick: boolean beforeNow = endTime. compareTo( new SimpleDateFormat("HH:mm:ss").


3 Answers

use Date objects. Date.setHours() allows you to specify hour, minutes, seconds

var currentD = new Date();
var startHappyHourD = new Date();
startHappyHourD.setHours(17,30,0); // 5.30 pm
var endHappyHourD = new Date();
endHappyHourD.setHours(18,30,0); // 6.30 pm

console.log("happy hour?")
if(currentD >= startHappyHourD && currentD < endHappyHourD ){
    console.log("yes!");
}else{
    console.log("no, sorry! between 5.30pm and 6.30pm");
}
like image 137
Gabo Esquivel Avatar answered Oct 09 '22 08:10

Gabo Esquivel


Assuming you are not interested in time zones and only need to compare two formatted time strings in 24h time format, you can just compare strings.

let time1 = '09:12';
let time2 = '12:22';

if (time1 > time2) {
 alert('yes')
} else {
 alert('no')
}

It works because strings in JS are compared symbol by symbol. So when compared '09:12' > '12:22' JS first checks if '0' == '1' and if false '0' > '1', and then returns result.

For comparison of strings '01' > '02', it will check if '0' == '0' then skip to next symbol in both strings and compare '1' > '2' which is false.

Strings has to be normalized to hh:mm, otherwise you will get unexpected results like:

"9:12" > "12:22" //true
like image 37
Igor Grechishkin Avatar answered Oct 09 '22 08:10

Igor Grechishkin


Date.parse('25/09/2013 13:31') > Date.parse('25/09/2013 9:15')

EDIT:

Note that you are parsing an arbitrary date that you're not interested in, it just needs to be the same on both sides.

like image 6
Arjun Sol Avatar answered Oct 09 '22 10:10

Arjun Sol