年月日的计算
// 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);
本文作者:心动12138
本文链接:https://www.cnblogs.com/7788mmhh/p/18220820
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步