<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>根据当前时间推算出前n个月的时间/指定月份</title>
</head>
<body>
<script>
var m = 1; //月份 (往前推几个月)
//月份必须转换为一个数字
if(parseInt(m) > 0){
var myDate = new Date();
var year = myDate.getFullYear(); //年
var month = myDate.getMonth()+1; //月
var day = myDate.getDate() //日
// var day = myDate.getDate() > 10 ? myDate.getDate() : '0' + myDate.getDate();//补零
var lowDate = ''
// ------------可忽略---------------
var ylow = parseInt(parseInt(m)/12);//往前推的总月份换成对应的年数取整
var mlow = parseInt(m) % 12; //往前推的月数
if(ylow > 0){
year = year - ylow;
}else{
year = year
}
// ------------可忽略--------------
//如果推的月份大于当前的 年份就减一
if((mlow > month) || (mlow == month)){
year = year - 1;
month = 12 - mlow + month;
lowDate = year + '年' + month + '月' + day + '日'
}else{
month = month - mlow;
lowDate = year + '年' + month + '月' + day + '日'
}
console.log(lowDate)
}else{
console.log(m)
}
</script>
</body>
</html>