问题描述
- 日期加减的问题!!!!!!!
-
恩 具体如图片上说的一样下面是我的js
//显示当前日期
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
//计算
function test(){
//日期格式必须yyyy-mm-dd,月和日不带0,如2011-3-2
var date1 = $("#EntTime36").val();
var date = new Date(date1);
var num = $('#Timelimit').val;
date.setDate(date.getDate()+num);
var date2 = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
$('#Maturitydate').val(date2);} 调用这个方法了 但是无法实现效果 哪里的问题 ![图片说明](http://img.ask.csdn.net/upload/201602/01/1454315190_331391.png) ![图片说明](http://img.ask.csdn.net/upload/201602/01/1454315251_630398.png)
解决方案
<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<script>
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + (parseInt(days)|| 0));
return this;
};
$(document).ready(function(){
//显示当前日期
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
});
//计算
function test(){$('#Maturitydate').val('');
var num = $('#Timelimit').val();
d = new Date();
var someDate = d.addDays(num );
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = y + '-'+ mm + '-'+dd;
$('#Maturitydate').val(someFormattedDate );
}
</script>
</head>
<body>
<p>购买日期 <input id='EntTime36' type='text'/></p>
<p>产品期限<input id='Timelimit' type='text' onblur='test()'/></p>
<p><p>预计到其日<input id='Maturitydate' type='text'/></p>
<p id='sum'> 计算</p>
</body>
</html>
解决方案二:
function test(){
//日期格式必须yyyy-mm-dd,月和日不带0,如2011-3-2
var date1 = $("#EntTime36").val();
var date = new Date(date1.replace(/-/g,'/'));////日期参数为字符串时参数为"年/月/日 时:分:秒",否则就是Date(年,月,日,时,分,秒)
var num = parseInt($('#Timelimit').val()) || 0;/////
date.setDate(date.getDate()+num);
var date2 = date.getFullYear()+'-'+(date.getMonth()+1)+'-'+date.getDate();
$('#Maturitydate').val(date2);
}
解决方案三:
<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<script>
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + parseInt(days));
return this;
};
$(document).ready(function(){
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
$("#sum").click(function(){
var num = $('#Timelimit').val();
var someDate = d.addDays(num );
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = y + '-'+ mm + '-'+dd;
$('#Maturitydate').val(someFormattedDate );
});
});
</script>
</head>
<body>
<p>购买日期 <input id='EntTime36' type='text'/></p>
<p>产品期限<input id='Timelimit' type='text'/></p>
<p><p>预计到其日<input id='Maturitydate' type='text'/></p>
<p id='sum'> 计算</p>
</body>
</html>
解决方案四:
你 的var num = $('#Timelimit').val;
这个少个括号$('#Timelimit').val();
解决方案五:
$(function(){
var startTimeStr = new Date().getTime(); //1970/01/01 至今的毫秒数
var limtDay=5;//假设限制天数是90天
var endTimeStr = startTime + limtDay * 3600 * 24 * 1000;//90天换算成毫秒
var lastTime= new Date();
lastTime.setTime(endTime);
console.log(lastTime.getFullYear() +'--'+lastTime.getMonth()+'--'+lastTime.getDate());
})
这个可以实现你的要求
解决方案六:
<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<script>
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + (parseInt(days|| 0)));
return this;
};
$(document).ready(function(){
//显示当前日期
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
});
//计算
function test(){$('#Maturitydate').val('');
var num = $('#Timelimit').val();
d = new Date();
var someDate = d.addDays(num );
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = y + '-'+ mm + '-'+dd;
$('#Maturitydate').val(isNaN(y)?'':someFormattedDate );
}
</script>
</head>
<body>
<p>购买日期 <input id='EntTime36' type='text'/></p>
<p>产品期限<input id='Timelimit' type='text' onblur='test()'/></p>
<p><p>预计到其日<input id='Maturitydate' type='text'/></p>
<p id='sum'> 计算</p>
</body>
</html>
解决方案七:
重新改了下。
<!DOCTYPE html>
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + (parseInt(days)|| 0));
return this;
};
$(document).ready(function(){
//显示当前日期
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
});
//计算
function test(){$('#Maturitydate').val('');
var num = $('#Timelimit').val();
d = new Date();
var someDate = d.addDays(num );
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = y + '-'+ mm + '-'+dd;
$('#Maturitydate').val(someFormattedDate );
}
购买日期
产品期限
预计到其日
解决方案八:
<!DOCTYPE html>
<html>
<head>
<script src="http://www.w3school.com.cn/jquery/jquery-1.11.1.min.js">
</script>
<script>
Date.prototype.addDays = function(days) {
this.setDate(this.getDate() + (parseInt(days)|| 0));
return this;
};
$(document).ready(function(){
//显示当前日期
var d = new Date();
var s2 = d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate();
$('#EntTime36').val(s2);
});
//计算
function test(){$('#Maturitydate').val('');
var num = $('#Timelimit').val();
d = new Date($('#EntTime36').val().split('-')[0],$('#EntTime36').val().split('-')[1]-1,$('#EntTime36').val().split('-')[2]);
var someDate = d.addDays(num );
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var someFormattedDate = y + '-'+ mm + '-'+dd;
$('#Maturitydate').val(someFormattedDate );
}
</script>
</head>
<body>
<p>购买日期 <input id='EntTime36' type='text'onblur='test()'/></p>
<p>产品期限<input id='Timelimit' type='text' onblur='test()'/></p>
<p><p>预计到其日<input id='Maturitydate' type='text'/></p>
</body>
</html>