Javascript日期和时间倒计时

时间:2014-09-24 03:30:33

标签: javascript date time

我必须创建一个拍卖风格的倒数计时器。我在javascript代码中预设了结束日期,然后我必须弄清楚如何计算差异。我的代码工作和计数很好,但我想看看是否有一种有效的方法来做到这一点。我从书中得到了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);
    }

1 个答案:

答案 0 :(得分:0)

请在您的文件中输入以下代码并应用所需日期。这对我来说可以。如果有任何问题,请告诉我。

<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>