js 日期计算 如:2021-10-01,需要在此日期上加减天数,得到新日期
作者:互联网
//beginDate 需要参加计算的日期,days是要减去的天数,返回新的日期,日期格式:YYYY-MM-DD
function getDay(beginDate, days) {
var beginDate = beginDate.split("-");
var nDate = new Date(beginDate[1] + '-' + beginDate[2] + '-' + beginDate[0]); //转换为MM-DD-YYYY格式
var millSeconds = Math.abs(nDate) - (days * 24 * 60 * 60 * 1000);
var rDate = new Date(millSeconds);
var year = rDate.getFullYear();
var month = rDate.getMonth() + 1;
if (month < 10) month = "0" + month;
var date = rDate.getDate();
if (date < 10) date = "0" + date;
return (year + "-" + month + "-" + date);
}
var res = getDay('2021-10-27',90)
console.log(res);
标签:10,01,beginDate,month,date,日期,rDate,var 来源: https://blog.csdn.net/Kane_qqq/article/details/121467383