// type:年、月或者日 // data:日期 // 加减量 function addOrReduceDate(type,date,num) { var nowDate = null; var strDate = ""; num = parseInt(num); // 防止传入字符串报错 var seperator1 = "-"; var seperator2 = ":"; if(date == "") { nowDate = new Date(); } else { nowDate = new Date(date); } if(type==="Y"){ nowDate.setFullYear(nowDate.getFullYear() + num); } if(type==="M"){ nowDate.setMonth(nowDate.getMonth() + num); } if(type==="D"){ nowDate.setDate(nowDate.getDate() + num); } if(type==="A"){ nowDate.setFullYear(nowDate.getFullYear() + num); nowDate.setMonth(nowDate.getMonth() + num); nowDate.setDate(nowDate.getDate() + num); } var year = nowDate.getFullYear(); // 年 var month = nowDate.getMonth() + 1; // 月 strDate = nowDate.getDate(); //日 var hours = nowDate.getHours(); // 时 var minutes = nowDate.getMinutes(); // 分 var seconds = nowDate.getSeconds(); // 秒 if(month >= 1 && month <= 9) { month = "0" + month; } if(strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } if(seconds >= 0 && seconds <= 9) { seconds = "0" + seconds; } // var dateStr = year + seperator1 + month + seperator1 + strDate + " " + hours + seperator2 + minutes + seperator2 + seconds; var dateStr = year + seperator1 + month + seperator1 + strDate; console.log(dateStr) return dateStr; } let resy= addOrReduceDate("M",formState.riqi,formState.youxiaoqi);
标签:num,month,date,nowDate,计算,var,年月日,type From: https://www.cnblogs.com/7788mmhh/p/18220820