我在SO中找不到解决方案所以我发布了这个问题。这是我如何获得时差,但我得到的是NaN
。我一直在寻找原因但无济于事。任何想法都表示赞赏。
var totaltime = "";
var check = true;
var timefrom = "1:00";
var timeto = "11:00";
//var timefrom = "10:00";
//var timeto = "19:00";
if (check) {
//console.log(toSeconds(data[0].timefrom))
var difference = Math.abs(toSeconds(timefrom) - toSeconds(timeto));
// format time differnece
var result = [
Math.floor(difference / 3600), // an hour has 3600 seconds
Math.floor((difference % 3600) / 60), // a minute has 60 seconds
difference % 60
];
// 0 padding and concatation
console.log(result)
totaltime = result.map(function(v) {
return v < 10 ? '0' + v : v;
}).join(':');
check = false;
}
console.log(totaltime)
function toSeconds(time_str) {
// console.log(time_str)
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return parts[0] * 3600 + // an hour has 3600 seconds
parts[1] * 60 + // a minute has 60 seconds
+
parts[2]; // seconds
}