简体   繁体   English

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

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

I have two date time pickers in Jquery. 我在Jquery中有两个日期时间选择器。

I need to show the difference on clicking the show button in the textbox with id result . 我需要显示单击id为result的文本框中的show按钮时的区别。

If 如果

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

Time2 = 05/01/2014 10:26 am Time2 = 05/01/2014 10:26 am

It should calculate its difference and show the result like X days Y Hrs Z minutes 它应该计算其差并显示结果,例如X days Y Hrs Z minutes

In addition I want the date to be in dd/mm/yy . 另外,我希望日期为dd/mm/yy Right now it is mm/dd/yy . 现在是mm/dd/yy

Fiddle 小提琴

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

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

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