I have two time strings in HH:MM:SS format. For example, str1
contains 10:20:45
, str2
contains 5:10:10
.
How can I compare the above values?
In this very case you can just compare strings date1. compareTo(date2) . EDIT: However, the proper way is to use SimpleDateFormat : DateFormat f = new SimpleDateFormat("yyyy-mm-dd"); Date d1 = f.
As Felix Kling said in the comments, provided your times are based on a 24 hour clock (and they should be if there's no AM/PM) and provided they are always in the format HH:MM:SS
you can do a direct string comparison:
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");
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With