How can I compare two time lines in the format HH: MM: SS?

I have two time lines in the format HH: MM: SS . For example, str1 contains 10:20:45 , str2 contains 5:10:10 .

How to compare the above values?

+43
javascript
Jun 02 2018-11-11T00:
source share
9 answers
 Date.parse('01/01/2011 10:20:45') > Date.parse('01/01/2011 5:10:10') > true 

January 1 - an arbitrary date, does not mean anything.

+73
Jun 02 '11 at 8:50
source share

As Felix Kling said in the comments, if your time is based on a 24-hour time (and it should be if there is no AM / PM) and provided that they are always in the HH:MM:SS format, you can do a direct comparison line:

 var str1 = "10:20:45", str2 = "05:10:10"; if (str1 > str2) alert("Time 1 is later than time 2"); else alert("Time 2 is later than time 1"); 
+84
Jun 02 2018-11-11T00:
source share

Try this code for a 24 hour time format.

 <script type="text/javascript"> var a="12:23:35"; var b="15:32:12"; var aa1=a.split(":"); var aa2=b.split(":"); var d1=new Date(parseInt("2001",10),(parseInt("01",10))-1,parseInt("01",10),parseInt(aa1[0],10),parseInt(aa1[1],10),parseInt(aa1[2],10)); var d2=new Date(parseInt("2001",10),(parseInt("01",10))-1,parseInt("01",10),parseInt(aa2[0],10),parseInt(aa2[1],10),parseInt(aa2[2],10)); var dd1=d1.valueOf(); var dd2=d2.valueOf(); if(dd1<dd2) {alert("b is greater");} else alert("a is greater"); } </script> 
+3
Jun 20 2018-12-12T00
source share

Try this code.

 var startTime = "05:01:20"; var endTime = "09:00:00"; var regExp = /(\d{1,2})\:(\d{1,2})\:(\d{1,2})/; if(parseInt(endTime .replace(regExp, "$1$2$3")) > parseInt(startTime .replace(regExp, "$1$2$3"))){ alert("End time is greater"); } 
+2
Jun 02 '11 at 8:50
source share

You can compare the two values โ€‹โ€‹immediately after they are divided by ":".

+2
Jun 02 2018-11-11T00:
source share

I don't really like regular expressions, and my example comes from the datetimepicker field formatted by m / d / Y h: mA. In this legitimate example, you need to arrive before the start of the hearing. I use the replace function to clear dates so that I can treat them as Date objects and compare them.

 function compareDateTimes() { //date format ex "04/20/2017 01:30PM" //the problem is that this format results in Invalid Date //var d0 = new Date("04/20/2017 01:30PM"); => Invalid Date var start_date = $(".letter #depo_arrival_time").val(); var end_date = $(".letter #depo_dateandtime").val(); if (start_date=="" || end_date=="") { return; } //break it up for processing var d1 = stringToDate(start_date); var d2 = stringToDate(end_date); var diff = d2.getTime() - d1.getTime(); if (diff < 0) { end_date = moment(d2).format("MM/DD/YYYY hh:mA"); $(".letter #depo_arrival_time").val(end_date); } } function stringToDate(the_date) { var arrDate = the_date.split(" "); var the_date = arrDate[0]; var the_time = arrDate[1]; var arrTime = the_time.split(":"); var blnPM = (arrTime[1].indexOf("PM") > -1); //first fix the hour if (blnPM) { if (arrTime[0].indexOf("0")==0) { var clean_hour = arrTime[0].substr(1,1); arrTime[0] = Number(clean_hour) + 12; } arrTime[1] = arrTime[1].replace("PM", ":00"); } else { arrTime[1] = arrTime[1].replace("AM", ":00"); } var date_object = new Date(the_date); //now replace the time date_object = String(date_object).replace("00:00:00", arrTime.join(":")); date_object = new Date(date_object); return date_object; } 
0
Apr 20 '17 at 17:34 on
source share

Date object in js support mapping, set the same date for them to compare hh: mm: ss:

 new Date ('1/1/1999 ' + '10:20:45') > new Date ('1/1/1999 ' + '5:10:10') > true 
0
Dec 21 '17 at 4:11
source share
 var str1 = "150:05:05", var str2 = "80:04:59"; function compareTime(str1, str2){ if(str1 === str2){ return 0; } var time1 = str1.split(':'); var time2 = str2.split(':'); for (var i = 0; i < time1.length; i++) { if(time1[i] > time2[i]){ return 1; } else if(time1[i] < time2[i]) { return -1; } } } 
-2
Sep 27 '17 at 19:12
source share
 var startTime = getTime(document.getElementById('startTime').value); var endTime = getTime(document.getElementById('endTime').value); var sts = startTime.split(":"); var ets = endTime.split(":"); var stMin = (parseInt(sts[0]) * 60 + parseInt(sts[1])); var etMin = (parseInt(ets[0]) * 60 + parseInt(ets[1])); if( etMin > stMin) { // do your stuff... } 
-3
Sep 09 '16 at 16:51
source share



All Articles