日期时间倒计时,删除小数并在每个类别中留出时间显示时间

时间:2014-11-04 06:19:32

标签: javascript html time countdown

最初,找到了我在这里倒计时问题的部分答案
(" Cymen"感谢您的回答!)

如何在剩余的剩余时间内防止倒计时使用小数?

另外,我怎么能这样做它只会说出分钟剩下的秒数,分钟只剩下小时数等等,就像在这个网站上碰巧有我确切计算的确切日期和时间一样....到 http://50onfire.com/dc/

以下是我编辑Cymen的代码:
http://jsfiddle.net/McdSV/

HTML

<div class="countdown"></div>
    <p class="labels">
        <span class="weeks">WEEKS </span>
        <span class="days">DAYS </span>
        <span class="hours">HOURS </span>
        <span class="minutes">MINUTES </span>
        <span class="seconds">SECONDS </span>
    </p>    

JS

$(document).ready(function () {
    var date = new Date(2014, 11, 4, 19);
    var $display = $('.countdown');
    countdown($display, date);
    setInterval(function () { countdown($display, date); }, 1000);
});

var offset = get_time_zone_offset();

function countdown($display, collision) {
    var now = new Date();
    now.setHours(now.getHours() + (offset-5));
    var seconds = Math.ceil((collision.getTime() - now.getTime()) * 0.001);
    var minutes = Math.ceil(seconds/60);
    var hours = Math.ceil(seconds/60/60 * 10)/10;
    var days = Math.ceil((seconds/60/60/24) * 100)/100;
    var weeks = Math.ceil((days/7) * 100)/100;
    $display.html
    (
        '<p class="numbers">' + 
        collision + ((offset != 5) ? ' with time zone offset of ' + (offset-5) + ' hours' : '') +
        '<br><br><span id="weeks">' + weeks + '</span>' + 
        '<span id="days">' + days + '</span>' +
        '<span id="hours">' +hours + '</span>' +
        '<span id="minutes">' +minutes + '</span>' +
        '<span id="seconds">' +seconds + '</span>' +
        '</p>'
    );
}

3 个答案:

答案 0 :(得分:0)

尝试在 $ display.html

之前添加以下代码
seconds = parseInt(seconds);
minutes = parseInt(minutes);
hours = parseInt(hours); 
days = parseInt(days);
weeks = parseInt(weeks); 

答案 1 :(得分:0)

检查一下:

http://harshen.github.io/jquery-countdownTimer/

他们提供了你想要的相同内容,同时也提供了很好的描述和教程。如果您愿意,也可以轻松修改代码。

我修改了你的演示:

http://jsfiddle.net/patelbharat001/McdSV/154/

在此我改变了你的过程以获得差异,

检查它,可以帮到你。

    var seconds1=Math.floor(((endDate-startDate)%(24*60*60*1000))/1000)%60%60;

    var seconds= ((60 + parseInt(seconds1)));

答案 2 :(得分:0)

你想这样吗?

Sat Jul 21 2012 15:30:00 GMT-0400 (Eastern Daylight Time)
seconds: 5
minutes: 0
hours: 13
days: 2
weeks: 119

JS小提琴链接: Demo

$(document).ready(function () {
    var date = new Date(2012, 6, 21, 15, 30);
    var $display = $('#countdown');
    countdown($display, date);
    setInterval(function () { countdown($display, date); }, 1000);
});

var offset = get_time_zone_offset();

function countdown($display, collision) {
    var now = new Date();
    now.setHours(now.getHours() + (offset-5));
    var seconds = Math.ceil((collision.getTime() - now.getTime()) * 0.001);
    var minutes = Math.ceil(seconds/60);
    var hours = Math.ceil(seconds/60/60 * 10)/10;
    var days = Math.ceil((seconds/60/60/24) * 100)/100;
    var weeks = Math.ceil((days/7) * 100)/100;
    //seconds = seconds - Math.floor(seconds / 60)*60

    $display
        .html
        (
            '<p>' + 
            collision + ((offset != 5) ? ' with time zone offset of ' + (offset-5) + ' hours' : '') +

            '<br>seconds: ' + Math.abs( Math.floor(-seconds / 60)*60 + seconds) +
            '<br>minutes: ' + Math.abs( Math.floor(-minutes / 60)*60 + minutes) +
            '<br>hours: ' + Math.floor(Math.abs( Math.floor(-hours / 24)*24 + hours)) +
            '<br>days: ' + Math.floor(Math.abs( Math.floor(-days / 7)*7 + days)) +
            '<br>weeks: ' + Math.floor(-weeks) +
            '</p>'
        );
}

function get_time_zone_offset() {
     var current_date = new Date( );
     var gmt_offset = current_date.getTimezoneOffset( ) / 60;
     return gmt_offset;
}