2
所以我做了一個函數來確定我要多久等到巴斯到達:時間到計算器給出錯誤的答案
function arrival(arrtime){
//convert to seconds
function toSeconds(time_str) {
// Extract hours, minutes and seconds
var parts = time_str.split(':');
// compute and return total seconds
return (parts[0] * 3600) + (parts[1] * 60) + parts[2];// seconds
}
var a = new Date().getHours() + ":" + new Date().getMinutes() + ":" + new Date().getSeconds();//current time
var difference = toSeconds(arrtime) - toSeconds(a);
function sformat(s) {
var fm = [
Math.floor(s/60/60/24), // DAYS
Math.floor(s/60/60) % 24, // HOURS
Math.floor(s/60) % 60, // MINUTES
s % 60 // SECONDS
];
return $.map(fm, function(v, i) { return ((v < 10) ? '0' : '') + v; }).join(':');
}
if (difference > 0){
result = sformat(difference);
} else if (difference < 1 && difference > -20) {
result = "Chegou!";
} else if (difference <= -20) {
result = "Amanhã às " + arrtime;
}
return result;
}
//usage example:
arrival("16:30:00");
,但它給我回答錯了.... 一些計算必須是錯的,但對於我的生活我無法弄清楚!
那個伎倆,謝謝! –