繁体   English   中英

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

[英]Difference between date and time in a date time picker Jquery

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

我需要显示单击id为result的文本框中的show按钮时的区别。

如果

时间1 = 04/30/2014 10:27上午

Time2 = 05/01/2014 10:26 am

它应该计算其差并显示结果,例如X days Y Hrs Z minutes

另外,我希望日期为dd/mm/yy 现在是mm/dd/yy

小提琴

小提琴演示

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');
});
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)');

小提琴

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM