0
我不得不創建拍賣風格倒計時器。我在JavaScript代碼中預設了結束日期,然後我必須弄清楚如何計算差異。我的代碼工作正常,但我希望看看是否有這樣做的有效方法。我從書中得到了日計算,但認爲沒有必要創建一個小時,分鐘,秒變量並且除以它。Javascript日期和時間倒計時
var day = 1000 * 60 * 60 *24;
// Initialize and set variables.
var startDate = new Date();
startDate.toTimeString();
var endDate = new Date('4 Oct 2014 12:30:00 UTC');
endDate.toTimeString();
var startHour = startDate.getHours();
var startMin = startDate.getMinutes();
var startSec = startDate.getSeconds();
var endHour = endDate.getHours();
var endMin = endDate.getMinutes();
var endSec = endDate.getSeconds();
// Make sure the start date comes first:
if (startDate < endDate) {
// Get the interval:
var diffDay = endDate - startDate;
if (diffDay <= day) {
intervalD = '1 day ';
} else {
intervalD = Math.round(diffDay/day) + ' days ';
}
if (endMin >= startMin){ // Make sure its counting down
var diffHour = Math.abs(endHour - startHour);
} else { //Since start hour is greater, we now have to count down from 60.
var diffHour = ((endHour - startHour) + 60);
}
if (endMin >= startMin){
var diffMin = Math.abs(endMin - startMin);
} else {
var diffMin = ((endMin - startMin) + 60);
}
if (endSec >= startSec){
var diffSec = Math.abs(endSec - startSec);
} else {
var diffSec = ((endSec - startSec) + 60);
}
如果您想要某人查看您的代碼,您應該詢問[codereview.se] – 2014-09-24 03:35:34
非常感謝您的提示! – EsotericRider 2014-09-24 05:10:17