I try compare two different time to produce the difference time.
var start_time;
var end_time;
start_time = $('#startTime').val();
end_time = $('#endTime').val();
values received are,
start_time : 5:08 PM
end_time : 6:08 PM
here, how can get a difference time as 1 hr from these time ?
Expected output: 01:00
Is this possible with JavaScript ?
Hope this snippet will be useful
function getDifference(t1, t2) {
//first splitting by white space. FOr example 5:08 PM will be 5:08 and again splitting by :
var _t1 = (t1.split(" ")[0].split(":")),
// converting to number to get the time in minute
toNum1 = Number(_t1[0]) * 60 + Number(_t1[1]),
_t2 = (t2.split(" ")[0].split(":")),
toNum2 = Number(_t2[0]) * 60 + Number(_t2[1]),
// calulating difference
dif = toNum2 - toNum1;
var result = ''
if (dif % 60 === 0) {
result = dif % 60 + ':00'
} else if (dif % 60 < 10) {
result = Math.floor(dif / 60) + ':0' + dif % 60
} else if (dif % 60 > 10) {
result = Math.floor(dif / 60) + ':' + dif % 60
}
return result
}
console.log(getDifference("5:08 PM", "6:59 PM"))
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments