new Date(2021,3,0).getDate()
格式固定 日要写0
请勿写成 "yyyy-MM-dd" 或者 "yyyy/MM/dd" 均会报错
那么为什么这么写可以呢
https://www.h5w3.com/140347.html
额外放一个uitl获取当前日期的 前几年 前几月 前几日
// num 是跨度数字 type 是 年|月|日 flag 是 +|- (代表向前减日期还是向后加日期)
$getTimeListByN(num= 0,type='月',flag= '-'){
var beginDate,currentDate,changeDate,endDate;
$.ajax({
type: "get",
url: "https://xxxxxxxxxxxxxx",
async: false,
dataType:"text",
success: function (result) {
// currentDate = new Date(JSON.parse(result).currentTime+"")
currentDate = new Date()
},
error: function (data) {
}
});
beginDate = currentDate.getFullYear() + '-' + (currentDate.getMonth() + 1) + '-' + currentDate.getDate();
switch (type) {
case "年":
changeDate = new Date(eval(currentDate.getFullYear()+flag+num),currentDate.getMonth(),currentDate.getDate());//修改后的时间戳
break
case "月":
changeDate = new Date(currentDate.getFullYear(), eval(currentDate.getMonth()+flag+num),currentDate.getDate());//修改后的时间戳
break
case "日":
// changeDate += " 00:00:00";//设置为当天凌晨12点
// changeDate = Date.parse(new Date(changeDate))/1000;//转换为时间戳
// var days = common.$getMonthDays(currentDate.getFullYear(),currentDate.getMonth() + 1)
// changeDate -= (86400) * num;//修改后的时间戳
// changeDate = new Date(parseInt(changeDate) * 1000);//转换为时间
changeDate = new Date(currentDate.getFullYear(),currentDate.getMonth(),eval(currentDate.getDate()+flag+num));//修改后的时间戳
break
}
endDate = changeDate.getFullYear() + '-' + (changeDate.getMonth() + 1) + '-' + changeDate.getDate();
return [endDate,beginDate]
},