把日期在數組中。請注意,在Javascript的月份是從零開始所以從0到11迭代數組和當日期是更大然後今天之間顯示天:
var calcPayDate = function() {
var payDates = [];
payDates.push(new Date(2011, 0, 1));
payDates.push(new Date(2011, 1, 15));
payDates.push(new Date(2011, 2, 12));
payDates.push(new Date(2011, 3, 20));
var today = new Date();
for (var i = 0; i < payDates.length; i++) {
if (payDates[i] > today) {
document.getElementById('countdownDiv').innerHTML = calcDays(payDates[i], today);
break;
}
}
}
var calcDays = function(date1, date2) {
// The number of milliseconds in one day
var ONE_DAY = 1000 * 60 * 60 * 24
// Convert both dates to milliseconds
var date1_ms = date1.getTime()
var date2_ms = date2.getTime()
// Calculate the difference in milliseconds
var difference_ms = Math.abs(date1_ms - date2_ms)
// Convert back to days and return
return Math.round(difference_ms/ONE_DAY)
}
的calcDays功能是一種功能發現this網站
這些日子被放在一個叫做「倒計時」的div中。
你有沒有嘗試把日期放入數組? – 2011-02-28 11:20:48
@Ash - 說實話,我對JavaScript一無所知。我希望鏈接到一個教程或類似的東西。 – dannymcc 2011-02-28 11:22:18