日期时间选择器Jquery中日期和时间之间的差异

时间:2014-04-30 05:02:44

标签: javascript jquery date

我在Jquery有两个日期时间选择器。

我需要点击文本框中标有show的{​​{1}}按钮来显示差异。

如果

Time1 = 04/30/2014 10:27

Time2 = 05/01/2014 10:26

它应计算其差异并显示结果,如result

另外我希望日期在X days Y Hrs Z minutes。现在是dd/mm/yy

Fiddle

2 个答案:

答案 0 :(得分:0)

Fiddle Demo

var time1 = $('#basic_example_1'), //cache selectors
    time2 = $('#basic_example_2'),
    result = $("#result");
$("#show").click(function () {
    var d1 = new Date(time1.val()), //convert to date object
        d2 = new Date(time2.val()), //convert to date object
        msec = d2.getTime() - d1.getTime(), //get difference in milliseconds
        hh = Math.floor(msec / 1000 / 60 / 60), //get hours
        dd = Math.floor(hh / 24); //calculate days
    msec -= hh * 1000 * 60 * 60; //remove hours from msec
    hh -= dd * 24; //remove days from hours
    var mm = Math.floor(msec / 1000 / 60); //get minutes
    result.val(dd + ' days ' + hh + ' Hrs ' + mm + ' minutes');
});

答案 1 :(得分:0)

var start = $('#basic_example_1').datepicker('getDate');
var end = $('#basic_example_2').datepicker('getDate');
var diff = new Date(end - start);
var days = Math.floor(diff/1000/60/60/24);
var hours = Math.floor((diff % ( 1000 * 60 * 60 * 24)) / 1000 / 60 / 60);
var minutes = Math.floor((diff % ( 1000 * 60 * 60)) / 1000 / 60);
$("#result").val(days+' Day(s) '+hours+' Hour(s) '+minutes+' Minute(s)');

Fiddle