js获取前几个月的具体日期
作者:互联网
// 往前数monthNum月份,不能往后数monthNum
export function GetTowFristDay(monthNum) { let nowdays = new Date(); let year = nowdays.getFullYear(); //获取当前日期的年份 let month = nowdays.getMonth(); //获取当前日期的月份 let day = nowdays.getDate(); //获取当前日期的日 let days = new Date(year, month, 0) days = days.getDate() //获取当前日期中月的天数 let year2 = year let month2 = parseInt(month) - monthNum if (month2 <= 0) { year2 = parseInt(year2) - parseInt(month2 / 12 == 0 ? 1 : Math.abs(parseInt(month2 / 12)) + 1) month2 = 12 - (Math.abs(month2) % 12) } let day2 = day let days2 = new Date(year2, month2, 0) days2 = days2.getDate() if (day2 > days2) { day2 = days2 } if (month2 < 10) { month2 = '0' + month2 } let t2 = year2 + '-' + month2 + '-' + day2 return t2 }标签:monthNum,js,获取,日期,let,nowdays,year,month,month2 来源: https://www.cnblogs.com/lisixuan/p/11770030.html