Since this is often used in projects, put the method to get the date in main.js and add the global method to vue. prototype for all pages to use.

Gets the last day of the month

Vue.prototype.getLastDay = function(yearmonth) { let month = yearmonth.split('-')[1] var new_year = yearmonth.split('-')[0]; Var new_month = month++; If (month > 12) {new_month -= 12; if (month > 12) {new_month -= 12; // month minus new_year++; Var new_date = new Date(new_year, new_month, 1); Return (new Date(new_date.getTime() -1000 * 60 * 60 * 24)).getDate(); // get the last day of the month}Copy the code

Last 7 days, last 30 days, yesterday, last 60 days, this month, last month, last 12 months, last 10 months, this week, this year

Vue. Prototype. SevenBefore = function (a type) {/ / new Date () gets the current Date and time of var myDate = new Date (); var beforeseven = new Date(); var thirty = new Date(); var sixty = new Date(); var custom = new Date(); Mydate.getdate () returns the current day of the month ~ for example, today is September 17, 2021, returns 17. The current mydate.setDate (mydate.getDate ()) is set to September 17, 2021. beforeseven.setDate(beforeseven.getDate()-1 - 6); thirty.setDate(thirty.getDate()-1 - 29); sixty.setDate(sixty.getDate() - 59); //getMonth returns the number of months, If (type === 0) {return [beforeseven.getFullYear() + '-' + Twodigits(beforeseven.getMonth() + 1)  + '-' + Twodigits(beforeseven.getDate()), myDate.getFullYear() + '-' + Twodigits(myDate.getMonth() + 1) + '-' + Twodigits(myDate.getDate()) ]; } else if (type === 1) {// last 30 days return [thirty.getFullYear() + '-' + Twodigits(thirty.getMonth() + '-' + Twodigits() + 1) + '-' + Twodigits(thirty.getDate()), myDate.getFullYear() + '-' + Twodigits(myDate.getMonth() + 1) + '-' + Twodigits(myDate.getDate()) ]; } else if (type === 2) {// yesterday var yesterday = this.getDay(-1, '-'); return [ yesterday, yesterday ]; } else if (type === 3) {// Return [sixty. GetFullYear () + '-' + Twodigits(sixty. GetMonth () + 1) + '-' + Twodigits(sixty.getDate()), myDate.getFullYear() + '-' + Twodigits(myDate.getMonth() + 1) + '-' + Twodigits(myDate.getDate()) ]; } else if (type === 4) {// this month var getMonthStartDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);  var monthEndDate = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()); return [formatDate(getLastMonthStartDate), formatDate(monthEndDate)]; } else if (type === 5) {// last month var lastMonthStartDate = new Date(new Date().getFullYear(), new Date().getMonth() -1, 1); var date = new Date(); var day = new Date(date.getFullYear(), date.getMonth(), 0).getDate(); var lastMonthEndDate = new Date(new Date().getFullYear(), new Date().getMonth() - 1, day); return [formatDate(lastMonthStartDate), formatDate(lastMonthEndDate)]; } else if (type === 6) {var dataArr = []; var data = new Date(); data.setMonth(data.getMonth() + 1); for (var i = 0; i < 12; i++) { data.setMonth(data.getMonth() - 1); dataArr.push(data.getFullYear() + '-' + Twodigits((data.getMonth()))); } return [dataArr[11], dataArr[0]]; } else if (type === 7) {var DataArr = []; var Data = new Date(); Data.setMonth(Data.getMonth() + 1); for (var ten = 0; ten < 10; ten++) { Data.setMonth(Data.getMonth() - 1); DataArr.push(Data.getFullYear() + '-' + Twodigits((Data.getMonth() + 1))); } return [DataArr[9], DataArr[0]]; } else if (type === 8) {var current=new Date(); var firstDay = new Date(current.getFullYear(),current.getMonth(),current.getDate()-current.getDay()+1); Return [moment(firstDay). Format ('YYYY-MM-DD'),moment(current). Format ('YYYY-MM-DD')]} else if (type === 9){return [moment(firstDay). Format ('YYYY-MM-DD'),moment(current) firstDay = new Date(); firstDay.setDate(1); firstDay.setMonth(0); var now = new Date(); return [moment(firstDay).format('YYYY-MM-DD'),moment(now).format('YYYY-MM-DD')] } }; function Twodigits(num) { return num < 10 ? '0' + num : num; } Vue.prototype.getDay = function (num, str) { var today = new Date(); var nowTime = today.getTime(); var ms = 24 * 3600 * 1000 * num; today.setTime(parseInt(nowTime + ms)); var oYear = today.getFullYear(); var oMoth = (today.getMonth() + 1).toString(); if (oMoth.length <= 1) oMoth = '0' + oMoth; var oDay = today.getDate().toString(); if (oDay.length <= 1) oDay = '0' + oDay; return oYear + str + oMoth + str + oDay; }; function formatDate(date) { var myYear = date.getFullYear(); var myMonth = date.getMonth() + 1; var myWeekday = date.getDate(); if (myMonth < 10) { myMonth = '0' + myMonth; } if (myWeekday < 10) { myWeekday = '0' + myWeekday; } return (myYear + '-' + myMonth + '-' + myWeekday); }Copy the code

Call it directly from the page you want to use.

console.log(this.sevenBefore(0),'0') console.log(this.sevenBefore(1),'1') console.log(this.sevenBefore(2),'2') console.log(this.sevenBefore(3),'3') console.log(this.sevenBefore(4),'4') console.log(this.sevenBefore(5),'5') console.log(this.sevenBefore(6),'6') console.log(this.sevenBefore(7),'7') console.log(this.sevenBefore(8),'8') console.log(this.sevenBefore(9),'9') ! [EC135DB1-F183-41A6-97E7-868C900ABBF4.png](https://p6-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0b4fedb6a9f74729b630ca2a6f2 36a82~tplv-k3u1fbpfcp-watermark.image?)Copy the code

How many (AA) days close to/after the current date

Vue.prototype.fun_date = function(aa){ var date1 = new Date(), time1=date1.getFullYear()+"-"+Twodigits(date1.getMonth()+1)+"-"+Twodigits(date1.getDate()); Var date2 = new Date(date1); date2.setDate(date1.getDate()+aa); var time2 = date2.getFullYear()+"-"+Twodigits(date2.getMonth()+1)+"-"+Twodigits(date2.getDate()); return time2; } console.log(this.fun_date(-2)) //2021-09-15Copy the code