js获取文本框的日期,并且进行加减,以得到另一个日期
发布网友
发布时间:2022-04-30 03:56
我来回答
共2个回答
热心网友
时间:2022-04-24 08:15
<script>
var d= new Date("2014-10-10");//这里日期是传递过来的,可以自己改
d.setDate(d.getDate() + 10);//天数+10
var nd=d.getFullYear()+"-"+(d.getMonth()+1)+"-"+d.getDate();//新日期
alert(nd);
</script>
热心网友
时间:2022-04-24 09:33
先把文本框A的值转为Date对象,对Date对象操作后将其转为字符串,再传给B
function changeDate() {
var offset = 10;
var dateStrA = txtA.value;
var year = dateStrA.substring(0,4);
var month = Number(dateStrA.substring(5,7))-1;
var date = Number(dateStrA.substring(8,10))+offset;
var dateB = new Date();
dateB.setFullYear(year,month,date);
var year2 = dateB.getFullYear();
var month2 = (dateB.getMonth()+1)+"";
var date2 = dateB.getDate()+"";
if (month2.length == 1) month2 = "0"+month2;
if (date2.length == 1) date2 = "0"+date2;
txtB.value = year2 + "-" + month2 + "-" + date2;
}