1、获取当前日期并想获取6个月前的日期
getDate() { var now = new Date(); var year = now.getFullYear(); //得到年份 var month = now.getMonth() + 1; //得到月份 var date = now.getDate(); //得到日期 var hour = " 00:00:00"; //默认时分秒 如果传给后台的格式为年月日时分秒,就需要加这个,如若不需要,此行可忽略 month = month.toString().padStart(2, "0"); date = date.toString().padStart(2, "0"); var defaultDate = `${year}-${month}-${date}`;//当前日期 var defaultDate1 = this.GetPreMonthDay(defaultDate,6);//6个月前 console.log(defaultDate1) this.QueryTime.push(defaultDate1,defaultDate) },
2、GetPreMonthDay 函数
GetPreMonthDay(date, monthNum) { var dateArr = date.split('-'); var year = dateArr[0]; //获取当前日期的年份 var month = dateArr[1]; //获取当前日期的月份 var day = dateArr[2]; //获取当前日期的日 var days = new Date(year, month, 0); days = days.getDate(); //获取当前日期中月的天数 var year2 = year; var month2 = parseInt(month) - monthNum; if (month2 <= 0) { var absM = Math.abs(month2); year2 = parseInt(year2) - Math.ceil(absM / 12 == 0 ? 1 : parseInt(absM) / 12); month2 = 12 - (absM % 12); } var day2 = day; var days2 = new Date(year2, month2, 0); days2 = days2.getDate(); if (day2 > days2) { day2 = days2; } if (month2 < 10) { month2 = '0' + month2; } var t2 = year2 + '-' + month2 + '-' + day2; return t2 },
标签:Vue,month2,month,date,日期,year,var,js From: https://www.cnblogs.com/ElvisZhongShao/p/17812546.html