使用Date对象和<输入类型=“;时间“>

Math operations with Date object and <input type="time">

本文关键字:时间 gt 类型 使用 对象 lt Date 输入      更新时间:2023-09-26

如何在JS中使用Date对象中的数据和HTML中的字符串("hh:mm")中的数据之间的数学运算?

代码用于使用getHours、getMinutes和getSeconds方法从字段中输入的时间到当前时间进行倒计时。或者最好使用一些其他标签和/或方法?

在从字段中获得数据后,使用jQuery插件进行倒计时怎么样?看看http://keith-wood.name/countdown.html.

编辑:

我使用以下JS代码http://jsfiddle.net/oxnmvhLc/2/:

function startCountdown(nowObj, timeObj) {
    setInterval(function() {
        var nowObj = new Date();
        if(nowObj > timeObj) {
            timeObj.setDate(timeObj.getDate() + 1);
        }
        var difference = (timeObj - nowObj) / 1000;
        var seconds = Math.floor(difference % 60);
        var minutes = Math.floor((difference/60) % 60);
        var hours = Math.floor((difference/3600) % 24);
        $('#countdown').html('There are ' + hours + ' hours, ' + minutes + ' minutes and ' + seconds + ' seconds.');
    }, 1000);
}
$('#btn').click(function() {
    var time = $('#time').val().split(':');
    var nowObj = new Date();
    var timeObj = new Date(nowObj.getFullYear(), nowObj.getMonth(), nowObj.getDate(), time[0], time[1], 0, 0);
    $('#countdown').html('');
    startCountdown(nowObj, timeObj);
});