我在 Jquery 中有两个日期时间选择器。
我需要显示单击 ID 为 result
的文本框中的 show
按钮时的差异。
如果
时间1 = 2014年4月30日上午10:27
时间2 = 2014年5月1日上午10:26
它应该计算其差异并显示结果,如 X 天 Y 小时 Z 分钟
此外,我希望日期采用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');
});
关于javascript - 日期时间选择器 Jquery 中日期和时间之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23380024/