使用js的两个时间选择器之间的时间差

时间:2015-08-05 03:57:48

标签: javascript timepicker

我使用两个时间选择器,它显示时差,但我想在文本框中的时差。 我的代码如下:

Start time: <input type="text" id="diff_time1" value=""><br>
End time: <input type="text" id="diff_time2" value=""><br>
<div id="diff_output">Time Difference
<input type='text' name='Time_diff'/></div>
<script type="text/javascript" src="ng_all.js"></script>
<script type="text/javascript" src="components/timepicker.js"></script>
<script type="text/javascript">
ng.ready( function() {
    ng.ready(function(){
        var tmpkr1 = new ng.TimePicker({
            input: 'diff_time1',
            events: {
                onSelect: calc_diff,
                onUnselect: calc_diff
            }
        });
        var tmpkr2 = new ng.TimePicker({
            input: 'diff_time2',
            events: {
                onSelect: calc_diff,
                onUnselect: calc_diff
            }
        });

        function calc_diff(){
            // getting the selected time values
            // value is a timestamp of the selected date
            // or null
            var tm1 = tmpkr1.p.value;
            var tm2 = tmpkr2.p.value;

            if ((!ng.defined(tm1)) || (!ng.defined(tm2))) {
                ng.get('diff_output').innerHTML = '';
                return;
            }

            var dif = Math.abs(tm1 - tm2); // difference in milliseconds
            var seconds = Math.round(dif/1000);
            var minutes = 0, hours = 0;
            if (seconds > 60) {
                minutes = Math.floor(seconds / 60);
                seconds = seconds - (minutes * 60);
            }
            if (minutes > 60){
                hours = Math.floor(minutes / 60);
                minutes = minutes - (hours * 60);    
            }

            var output = '';
            if (hours > 0) output = hours +' hours, ';
            if ((hours > 0) || (minutes > 0)) output += minutes+' minutes and ';
            output += seconds + ' seconds';

            ng.get('diff_output').innerHTML = output;
        };
    });
});
</script>

我想比较我从时间戳获得的手动时间差。如果时差相等,那么我将流程转移到下一页,我输入的时间是整数格式

2 个答案:

答案 0 :(得分:0)

很难理解你想做什么。

你能描述一下这个任务吗?

据我了解你有两个时间选择器,对吧?

用户在您的网页上选择两次,然后选择什么?

你想计算这两次之间的时差吗?

然后你想把这个评估的时差与一些恒定的时间戳进行比较,如果它们相等,你想把流转移到下一页,对吗?

答案 1 :(得分:0)

var secondsPerMinute = 60;
var minutesPerHour = 60;
function convertSecondsToHHMMSS(intSecondsToConvert) {
    var hours = convertHours(intSecondsToConvert);
    var minutes = getRemainingMinutes(intSecondsToConvert);
    minutes = (minutes == 60) ? "00" : minutes;
    var seconds = getRemainingSeconds(intSecondsToConvert);
    return hours + ":" + minutes;
}

function convertHours(intSeconds) {
    var minutes = convertMinutes(intSeconds);
    var hours = Math.floor(minutes / minutesPerHour);
    return hours;
}
function convertMinutes(intSeconds) {
    return Math.floor(intSeconds / secondsPerMinute);
}
function getRemainingSeconds(intTotalSeconds) {
    return (intTotalSeconds % secondsPerMinute);
}
function getRemainingMinutes(intSeconds) {
    var intTotalMinutes = convertMinutes(intSeconds);
    return (intTotalMinutes % minutesPerHour);
}

function HMStoSec1(T) { // h:m:s
    var A = T.split(/\D+/); return (A[0] * 60 + +A[1]) * 60 + +A[2]
}

使用以下代码处理差异

var time2 = HMStoSec1('17:00:00');
var time1 = HMStoSec1('08:00:00');
if(time1 < time2) {
    var diff = (time2 - time1);
    var result = convertSecondsToHHMMSS(diff);
} else {
    var diff = (time1 - time2);
    var result = convertSecondsToHHMMSS(24 * 60 * 60 - diff);
}