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);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…