JQuery Datepicker - 添加年份 [英] JQuery Datepicker - Add Years
问题描述
他们还可以使用Datepicker选择他们购买设备的日期,然后从下拉列表中选择保修期限(即1,2,3,4 ,5年等)。根据他们选择的保修期限,我需要JQuery代码来确定设备的保修期限到期日,即如果购买21/02/2011,3年warrent,则保修期将于2011年2月21日到期。 / p>
我已经查看了这个网站上的其他Datepicker示例,但没有一个显示如何添加几年的日期,而不是如何添加日子。
这是我的代码到目前为止
$('#equipment_purchaseDate')datepicker ({dateFormat:'dd / mm / yy',onSelect:function(dateStr){
var d = $ .datepicker.parseDate('dd / mm / yy',dateStr);
d.setDate (d.getDate()+($('#equipment_warrantyLength')。val()* 365)); //多个保修期长达365年,然后添加到startDate
$('#equipment_warrentyExpires') .datepicker('setDate',d);
}
});
但这似乎无法正常工作。
任何反馈将不胜感激。
谢谢。
尝试使用 Date
的 setFullYear
和 getFullYear
:
$(#device_purchaseDate)。datepicker({
dateFormat:'dd / mm / yy',
onSelect:function(dateStr){
var d = $ .datepicker.parseDate(' dd / mm / yy',dateStr);
var years = parseInt($(#equipment_warrantyLength)。val(),10);
d.setFullYear(d.getFullYear )+年);
$('#equipment_warrentyExpires')。datepicker('setDate',d);
}
});
这是一个工作示例: http://jsfiddle.net/andrewwhitaker/Dvd5C/
I have added the JQuery Datepicker control to one of my ASP.Net pages. The page allows a user to add details about a piece of equipment.
They can also select the date they bought the equipment using the Datepicker, then from a drop down list, select the warranty length (ie, 1,2,3,4,5 years etc) that comes with the equipment. Based on the warranty length they have selected, I need JQuery code to work out the date the warranty will expire for the Equipment, ie, if bought 21/02/2011, 3 year warrenty, then warranty expires 21/02/2014.
I have looked at the other Datepicker examples on this site, but none seem to show how to add years to a date, rather just how to add days.
This is my code so far
$('#equipment_purchaseDate').datepicker({ dateFormat: 'dd/mm/yy', onSelect: function (dateStr) {
var d = $.datepicker.parseDate('dd/mm/yy', dateStr);
d.setDate(d.getDate() + ($('#equipment_warrantyLength').val() * 365)); //multiple warranty length by 365 to get years, then add to startDate
$('#equipment_warrentyExpires').datepicker('setDate', d);
}
});
However this doesn't seem to be working correctly.
Any feedback would be much appreciated.
Thanks.
Try using Date
's setFullYear
and getFullYear
:
$("#equipment_purchaseDate").datepicker({
dateFormat: 'dd/mm/yy',
onSelect: function(dateStr) {
var d = $.datepicker.parseDate('dd/mm/yy', dateStr);
var years = parseInt($("#equipment_warrantyLength").val(), 10);
d.setFullYear(d.getFullYear() + years);
$('#equipment_warrentyExpires').datepicker('setDate', d);
}
});
Here's a working example: http://jsfiddle.net/andrewwhitaker/Dvd5C/
这篇关于JQuery Datepicker - 添加年份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!