使用开始日期计算结束日期

时间:2015-11-25 11:02:28

标签: jquery jquery-ui-datepicker

我有三个输入字段如下:

Warranty Start Date : <input id="WarrantyStartDate"  value="2015-11-22" />
Warranty period : <input id="WarrantyStartDate" value="24"/> //24 months//
Warranty End Date : <input id="WarrantyEndDate" />

现在我的问题是如何使用保修期值自动计算结束日期。 在简单中我想要在此日期24个月后的日期“2015-11-22”

我创造了一个js-fiddle:

http://jsfiddle.net/3DzTu/14/

解决:

function getEndDate(){

var warranty_period = $('#WarrentyPeriod').val();
var warranty_value = parseInt(warranty_period); 

var WarrentyStartDate = $('#WarrentyStartDate').val();
var WarrentyStartDate = WarrentyStartDate.split('-');
var WarrentyStartDate = WarrentyStartDate[0]+'/'+WarrentyStartDate[1]+'/'+WarrentyStartDate[2];
var dateString = WarrentyStartDate; // date string
var actualDate = new Date(dateString); // convert to actual date
var newDate = new Date(actualDate.getFullYear(), actualDate.getMonth()+warranty_value, actualDate.getDate());
var myDate = new Date(newDate);
var myDate = myDate.toLocaleString();
var myDate = myDate.split(',');
var myDate = myDate[0];
var myDate = myDate.split('/');
var year = myDate[2];
var month = '';
var date = '';

if(myDate[0].length < 2){

    month = '0'+myDate[0];
}else{
    month = myDate[0];

}


if(myDate[1].length < 2){

    date = '0'+myDate[1];
}else{
    date = myDate[1];

}

var WarrentyEndDate = year+'-'+month+'-'+date;


 $( "#WarrentyEndDate" ).val(WarrentyEndDate);


}

3 个答案:

答案 0 :(得分:3)

array(1) { ["fdata"]=> array(4) { [0]=> object(stdClass)#19 (4) { ["Type ID"]=> string(4) "ET01" ["Type Name"]=> string(30) "Teaching " ["Type Code"]=> string(2) "T " ["Type Status"]=> string(10) "Active " } [1]=> object(stdClass)#20 (4) { ["Type ID"]=> string(4) "ET02" ["Type Name"]=> string(30) "Non Teaching " ["Type Code"]=> string(2) "N " ["Type Status"]=> string(10) "Active " } [2]=> object(stdClass)#21 (4) { ["Type ID"]=> string(4) "ET03" ["Type Name"]=> string(30) "Contract " ["Type Code"]=> string(2) "C " ["Type Status"]=> string(10) "Locked " } [3]=> object(stdClass)#22 (4) { ["Type ID"]=> string(4) "ET04" ["Type Name"]=> string(30) "Short Contract " ["Type Code"]=> string(2) "SC" ["Type Status"]=> string(10) "Active " } }

答案 1 :(得分:2)

假设您的保修期为&#34; ID为&#34; WarrantyPeriod&#34;,您可以尝试以下代码:

var date = new Date($('#WarrantyStartDate').val());

date.setMonth(date.getMonth() + parseInt($('#WarrantyPeriod').val()));

$("#WarrantyEndDate").val(date.toISOString().slice(0,10));

答案 2 :(得分:0)

var startTime = new Date($("#WarrantyStartDate").datepicker().val()).getTime();
var period = new Date(0).setMonth(24);
var end = new Date(startTime + period);
$("#WarrantyEndDate").datepicker().val(end.getMonth() + "/" + end.getDate() + "/" + end.getFullYear());