How to compare time in javascript?

I have two times in the format "HH: MM", I want to compare them. I have the following code to get the time 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 should get the time after subtracting the difference by GMT:

 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 these values ​​as time when they are a string?

+11
source share
6 answers

use Date objects. Date.setHours () allows you to specify the 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"); } 
+13
source
 Date.parse('25/09/2013 13:31') > Date.parse('25/09/2013 9:15') 

EDIT:

Please note that you are parsing an arbitrary date that does not interest you, it should be the same on both sides.

+4
source
  if(Date.parse('01/01/2011 10:20:45') == Date.parse('01/01/2011 5:10:10')) { alert('same'); }else{ alert('different'); } The 1st January is an arbitrary date, doesn't mean anything. 
+1
source

we can do a hack with.

 var time1 = "09:30"; var time2 = "10:30"; var time1Date= new Date("01/01/2000 "+time1); var time2Date= new Date("01/01/2000 "+time2); if(time1Date >= time2Date ){ console.log("time1"); }else{ console.log("time2"); } 
+1
source

If I had enough representatives to vote for @Gabo Esquivel's decision. For several days I searched and tested the solutions, and this is the only thing that worked for me.

I needed to test a conditional statement if the current time is 0830, and if so, do something. The My if statement did not work, so I need other examples to work.

 //Business Hours: Saturday 8:30am-12pm; highlight Saturday table row. function showSaturdayHours() { var today = new Date(); var weekday = today.getDay(); var saturdayOpen = new Date(); saturdayOpen.setHours(8, 30, 0); var saturdayClose = new Date(); saturdayClose.setHours(12, 0, 0); if (weekday == 6) { $('#saturday-row').addClass('row-blue'); //highlight table row if current day is Saturday. if (today >= saturdayOpen && today < saturdayClose) { document.getElementById('saturday-open').innerHTML = 'Open'; } else { document.getElementById('saturday-open').innerHTML = 'Closed'; } } } 

Working hours table: JSFiddle

0
source

Like @Arjun Sol, instead of using Date.parse, you can simply extract the time from the string itself, create a new Date object and do the comparison.

 const time1 = '12:42'; const time2 = '18:30'; const getTime = time => new Date(2019, 9, 2, time.substring(0, 2), time.substring(3, 5), 0, 0); const result = getTime(time1) < getTime(time2); console.log('This should be true:', result); 
0
source

Source: https://habr.com/ru/post/954597/