jQuery 如何使用jquery datepicker向日期添加一天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2021788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add a day to a date using jquery datepicker
提问by Richard Reddy
I have 2 textboxes on my site for pickup date and drop off date both using the jquery date picker.
我的网站上有 2 个文本框,用于使用 jquery 日期选择器的取货日期和还车日期。
I am having trouble setting the drop off date value to be one day ahead of the pickup date that was selected.
我无法将还车日期值设置为所选取件日期前一天。
Here is what I have:
这是我所拥有的:
$('.pickupDate').change(function() {
var date2 = $('.pickupDate').datepicker('getDate', '+1d');
$('.dropoffDate').datepicker('setDate', date2);
});
The above will execute but the value in the drop off textbox will match the pickup value instead of being one day ahead. eg: if I select 01-01-2010 the above code returns 01-01-2010 in the drop off box instead of 02-01-2010.
以上将执行,但下车文本框中的值将与取件值匹配,而不是提前一天。例如:如果我选择 01-01-2010,上面的代码在下拉框中返回 01-01-2010 而不是 02-01-2010。
Any thoughts?
有什么想法吗?
Thanks for your help, Rich
感谢您的帮助,丰富
回答by Vincent Ramdhanie
Try this:
尝试这个:
$('.pickupDate').change(function() {
var date2 = $('.pickupDate').datepicker('getDate', '+1d');
date2.setDate(date2.getDate()+1);
$('.dropoffDate').datepicker('setDate', date2);
});
回答by selu220
This answer really helped me get started (noob) - but I encountered some weird behavior when I set a start date of 12/31/2014 and added +1 to default the end date. Instead of giving me an end date of 01/01/2015 I was getting 02/01/2015 (!!!). This version parses the components of the start date to avoid these end of year oddities.
这个答案确实帮助我开始(菜鸟) - 但是当我将开始日期设置为 12/31/2014 并添加 +1 以默认结束日期时,我遇到了一些奇怪的行为。而不是给我 01/01/2015 的结束日期,我得到的是 02/01/2015 (!!!)。此版本解析开始日期的组成部分,以避免这些年末的奇怪现象。
$( "#date_start" ).datepicker({
minDate: 0,
dateFormat: "mm/dd/yy",
onSelect: function(selected) {
$("#date_end").datepicker("option","minDate", selected); // mindate on the End datepicker cannot be less than start date already selected.
var date = $(this).datepicker('getDate');
var tempStartDate = new Date(date);
var default_end = new Date(tempStartDate.getFullYear(), tempStartDate.getMonth(), tempStartDate.getDate()+1); //this parses date to overcome new year date weirdness
$('#date_end').datepicker('setDate', default_end); // Set as default
}
});
$( "#date_end" ).datepicker({
minDate: 0,
dateFormat: "mm/dd/yy",
onSelect: function(selected) {
$("#date_start").datepicker("option","maxDate", selected); // maxdate on the Start datepicker cannot be more than end date selected.
}
});
回答by Daniel Moura
The datepicker('setDate') sets the date in the datepicket not in the input.
datepicker('setDate') 在 datepicket 中而不是在输入中设置日期。
You should add the date and set it in the input.
您应该添加日期并将其设置在输入中。
var date2 = $('.pickupDate').datepicker('getDate');
var nextDayDate = new Date();
nextDayDate.setDate(date2.getDate() + 1);
$('input').val(nextDayDate);