I have time in the format "hh:mm" in 24 hr format. Now how to compare two times such that it returns the boolean value . And if both the times is same in hours then should compare minutes and return either true or false. Meaning t1="15:30" and t2="15:40" should return false if checking whether t1> t2 using javascript
Asked
Active
Viewed 538 times
-5
-
http://stackoverflow.com/questions/6632808/compare-two-time-hhmmss-strings . Next time search before posting. :) – gsamaras May 02 '14 at 10:18
-
Native JS Date objects can be compared using `>`, `==` and `<` – Tom Fenech May 02 '14 at 10:18
-
124 hour format has the advantage that if you have them in string form, they'll compare correctly by just the regular string comparison operators. – Joachim Isaksson May 02 '14 at 10:19
2 Answers
0
Dates that are formatted HH:mm
(ie always 2 digit hours) with 24 hours time will compare correctly as strings without converting them;
var t1 = "15:30";
var t2 = "15:40";
console.log(t1 < t2);
console.log(t1 == t2);
console.log(t1 > t2);
> true
> false
> false

Joachim Isaksson
- 176,943
- 25
- 281
- 294
-
and if it is in 12 hr format that ia 10:30 AM , does that comparison work – user13763 May 02 '14 at 10:41
-
@user13763 No, only 24 hour format as stated in the question. – Joachim Isaksson May 02 '14 at 11:02