[英]Javascript Date and Time countdown
I had to create an auction style countdown timer. 我必须创建一个拍卖风格的倒数计时器。 I preset the end date in the javascript code and then I had to figure out how to calculate the difference.
我在javascript代码中预设了结束日期,然后不得不弄清楚如何计算差异。 My code works and count downs fine but I want to see if there was an efficient way of doing this.
我的代码可以正常工作,并且倒计时很好,但是我想看看是否有一种有效的方法来做到这一点。 I got the Day calculation from the book but thought it was unnecessary to create a hour, minute, sec variable and divide by it.
我从书中得到了Day的计算,但是认为没有必要创建时,分,秒变量并除以它。
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);
}
Please but below code in your file and apply the desire date. 请但在文件中的代码下方,然后应用所需日期。 It works fine for me.
这对我来说可以。 Let me know if any issue.
让我知道是否有任何问题。
<body>
<form name="count">
<input type="text" size="69" name="count2">
</form>
<script type="text/javascript">
//change the text below to reflect your own,
var before = "Christmas!"
var current = "Today is Christmas. Merry Christmas!"
var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
function countdown(yr, m, d) {
theyear = yr; themonth = m; theday = d
var today = new Date()
var todayy = today.getYear()
if (todayy < 1000)
todayy += 1900
var todaym = today.getMonth()
var todayd = today.getDate()
var todayh = today.getHours()
var todaymin = today.getMinutes()
var todaysec = today.getSeconds()
var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec
futurestring = montharray[m - 1] + " " + d + ", " + yr
dd = Date.parse(futurestring) - Date.parse(todaystring)
dday = Math.floor(dd / (60 * 60 * 1000 * 24) * 1)
dhour = Math.floor((dd % (60 * 60 * 1000 * 24)) / (60 * 60 * 1000) * 1)
dmin = Math.floor(((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) / (60 * 1000) * 1)
dsec = Math.floor((((dd % (60 * 60 * 1000 * 24)) % (60 * 60 * 1000)) % (60 * 1000)) / 1000 * 1)
if (dday == 0 && dhour == 0 && dmin == 0 && dsec == 1) {
document.forms.count.count2.value = current
return
}
else
document.forms.count.count2.value = "Only " + dday + " days, " + dhour + " hours, " + dmin + " minutes, and " + dsec + " seconds left until " + before
setTimeout("countdown(theyear,themonth,theday)", 1000)
}
//enter the Future count down date using the format year/month/day
countdown(2016, 9, 24)
</script>
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.