How to Compare Two Time Strings in the Format Hh:Mm:Ss

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

Date.parse('01/01/2011 10:20:45') > Date.parse('01/01/2011 5:10:10')
> true

The 1st January is an arbitrary date, doesn't mean anything.

Compare two time (hh:mm:ss) strings

DEMO HERE

I prefer to have date objects, but as pointed out elsewhere, you can just convert to seconds if you want to do simple compares

function dateCompare(time1,time2) {
var t1 = new Date();
var parts = time1.split(":");
t1.setHours(parts[0],parts[1],parts[2],0);
var t2 = new Date();
parts = time2.split(":");
t2.setHours(parts[0],parts[1],parts[2],0);

// returns 1 if greater, -1 if less and 0 if the same
if (t1.getTime()>t2.getTime()) return 1;
if (t1.getTime()<t2.getTime()) return -1;
return 0;
}

alert(dateCompare("12:40:13","20:01:01"));

For seconds:

function dateDiff(time1,time2) {
var t1 = new Date();
var parts = time1.split(":");
t1.setHours(parts[0],parts[1],parts[2],0);
var t2 = new Date();
parts = time2.split(":");
t2.setHours(parts[0],parts[1],parts[2],0);

return parseInt(Math.abs(t1.getTime()-t2.getTime())/1000);
}

Assuming you have 24 hour times and same padding you can do simple string compare

 var t1 = "12:40:13", t2= "20:01:01";
if (t1<t2) {
console.log(t1," is < ", t2);
}

How to compare times as strings in the format HH:MM:SS?

You can parse the given time strings into LocalTime and then use LocalTime#isBefore and LocalTime#isAfter to find the eligibility.

Demo:

import java.time.LocalTime;

public class Main {
static final String startTime = "13:50:00";
static final String endTime = "14:50:00";

public static void main(String[] args) {
// Test
System.out.println(canLogin("13:50:00"));
System.out.println(canLogin("14:30:00"));
System.out.println(canLogin("14:55:00"));
System.out.println(canLogin("14:50:00"));
}

static boolean canLogin(String arrivalTime) {
LocalTime start = LocalTime.parse(startTime);
LocalTime end = LocalTime.parse(endTime);
LocalTime arrival = LocalTime.parse(arrivalTime);
return !arrival.isAfter(end) && !arrival.isBefore(start);
}
}

Output:

true
true
false
true

ONLINE DEMO

Learn more about the modern Date-Time API* from Trail: Date Time.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

Compare two time in MM:SS format

As LocalTime seems to be unable to use a DateTimeFormatter pattern of mm:ss, you can also make a short static helper method that is called instead.

This method sets a default hour for all parses to the same value, which does not expose the "00:" anywhere else which could be bug prone if a different hour is accidentally entered.

This will ensure any Duration.between() for minutes and seconds will be accurate:

public static void main(String [] args) {
LocalTime start = parseHelper("20:28");
LocalTime stop = parseHelper("20:18");
Duration duration = Duration.between(start, stop);
System.out.println(duration.getSeconds());
}

private static LocalTime parseHelper(String str) {
return LocalTime.parse("00:" + str);
}

Output:

-10

EDIT:

Here are some docs that reinforce that hours are not optional in LocalTime, and apparently neither are minutes.

Here you can see they list the possible values in the toString() method.

The output will be one of the following ISO-8601 formats:

HH:mm
HH:mm:ss
HH:mm:ss.SSS
HH:mm:ss.SSSSSS
HH:mm:ss.SSSSSSSS

Improvement to this code to compare 2 time in format (HH:MM:SS)

Generally speaking it's safer to compare two Date objects than it is to compare strings.

You can do something like this:

// Get current date/time
var now = new Date();

// Set up deadline date/time
var deadline = new Date();
deadline.setHours(16);
deadline.setMinutes(0);

// Check if the current time is after the deadline
if( now > deadline ) {
alert('after deadline');
}
else {
alert('before deadline');
}

http://jsfiddle.net/md63mbpd/

Comparing two time strings in java

You can do it in that way:

 LocalTime time1 = LocalTime.parse("02:03:45");
LocalTime time2 = LocalTime.parse("10:04:20");

an then call: time1.compareTo(time2)

Comparing two Time in Strings

You can find the duration using

    String startTime = "10:00";
String endTime = "12:00";
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
Date d1 = sdf.parse(startTime);
Date d2 = sdf.parse(endTime);
long elapsed = d2.getTime() - d1.getTime();
System.out.println(elapsed);

Compare two time values (hh:mm am/pm)

If you add a 24h time value attribute to the option elements, like

<select id="eventstarttime">
<option value="1000">10:00am</option>
<option value="1215">12:15pm</option>
<option value="1500">3:00pm</option>
</select>

<select id="eventstoptime" onblur="return checktime()">
<option value="1000">10:00am</option>
<option value="1215">12:15pm</option>
<option value="1500">3:00pm</option>
</select>

you can easily compare them using

function checktime()
{
var start = document.getElementById("eventstarttime").value;
var end = document.getElementById("eventstoptime").value;

if (end < start)
{
alert("End time should exceed the start time");
}
else if (end == start)
{
alert("Start time and end time cannot be same");
}
return false;
}

No need for JavaScript Date methods.



Related Topics



Leave a reply



Submit