我需要添加几个月,所以我使用date.getMonth() +8;
和星期六,但我需要消除周末。如果我添加以下代码行:
if(date.getDay() % 6)
var date.getdate()+1;
它不起作用。那我该怎么办? 在这里摆弄https://jsfiddle.net/a3f3yb0s/ 这里是片段
var date = new Date();
var day = date.getDate();
var month = date.getMonth() +8;
var year = date.getFullYear();
var year2 = date.getFullYear() +1;
if(month>12) {
month=month%12;
year=year2;
}
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
var today = year + "-" + month + "-" + day;
document.getElementById('theDate').value = today;
<input type="date" id="theDate">
答案 0 :(得分:0)
为什么不在日期对象上进行计算,让浏览器处理所有日历计算:
// get a date object
// 8 months from 2016-01-01 is a Saturday
var date = new Date(2016, 1, 1);
// add 8 months
date.setMonth(date.getMonth() + 8);
alert(date); // will show a Saturday
// check if it is a saturday
if(date.getDay() == 6)
{
// if it is then add two days
date.setDate(date.getDate() + 2);
}
alert(date); // will show a Monday