问题描述
我需要计算所选日期和当前日期之间的差异周数。我试图用 weekNumberPicked - weekNumberCurrent
计算,但如果两个日期在不同的年份,结果是不正确的,所以我可能需要像 daysDifference / 7
。如何使用 onSelect
操作来实现?
您可以使用Datepicker的函数getDate获取Date对象。
然后从另一个减去一个日期(也许想要获得绝对值)以获得差异的毫秒,并计算差异以天或星期计算。
$ b $ ($ {
onSelect:function(){
var date = $(this).datepicker('getDate ');
var today = new Date();
var dayDiff = Math.ceil((今天 - 日期)/(1000 * 60 * 60 * 24));
}
});
I need to calculate the number of weeks difference between the chosen date and the current date. I've tried to calculate with weekNumberPicked - weekNumberCurrent
, but if the two dates are in different years, the result is incorrect, so I probably need to get it like daysDifference / 7
. How should I implement this with the onSelect
action?
You can use the Datepicker's function getDate to get a Date object.
Then just subtract one date from the other (might want to get the absolute value as well) to get the milliseconds in difference, and calculate the difference in days, or weeks.
$('#test').datepicker({
onSelect: function() {
var date = $(this).datepicker('getDate');
var today = new Date();
var dayDiff = Math.ceil((today - date) / (1000 * 60 * 60 * 24));
}
});
这篇关于jQuery UI Datepicker差异天数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!