Get nearly a week
var end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; Var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; If (day-7 <= 0) {// If (day-7 <= 0) {var startMonthDay = new Date(year, (parseInt(month) -1), 0). Dateobj. start = (year - 1) + '-' + 12 + '-' + (31 - (7-day)); dateobj. start = (year - 1) + '-' + 12 + '-' + (31 - (7-day)); } else { dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (7 - day)); } } else { dateObj.start = year + '-' + month + '-' + (day - 7); } the console. The log (JSON. Stringify (dateObj)) 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.Copy the code
It takes nearly a month
var end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; Var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; var endMonthDay = new Date(year, month, 0).getDate(); DateObj. Start = (year - 1) + '-' + 12 + '-' + day; }else{ var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate(); If (startMonthDay < day){// The total number of days 1 month ago is less than the current number of days if(day < endMonthDay){// The total number of days in the current month is less than the total number of days in the current month dateobj. start = year + '-' + (month - 1) + '-' + (startMonthDay - (endMonthDay - day)); }else{ dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay; } }else{ dateObj.start = year + '-' + (month - 1) + '-' + day; }} to the console. The log (JSON. Stringify (dateObj)) 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17.18.19.20.21.22.Copy the code
It takes nearly three months
var end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; Var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; var endMonthDay = new Date(year, month, 0).getDate(); If (month-3 <= 0){// If (month-3 <= 0){// If (month-3 <= 0){// If (month-3 <= 0); Var start3MonthDay = new Date((year-1), (12 - (3-parseInt (month))), 0).getDate(); // Total number of days 3 months ago if(start3MonthDay < day){// Total number of days 3 months ago is less than the current date dateobj. start = (year-1) + '-' + (12 - (3-month)) + '-' + start3MonthDay; }else{ dateObj.start = (year - 1) + '-' + (12 - (3 - month)) + '-' + day; } }else{ var start3MonthDay = new Date(year, (parseInt(month) - 3), 0).getDate(); // Total number of days 3 months ago if(start3MonthDay < day){// Total number of days 3 months ago is less than the current number of days if(day < endMonthDay){// The current number of days is less than the current number of days dateObj.start = year + '-' + (month - 3) + '-' + (start3MonthDay - (endMonthDay - day)); }else{ dateObj.start = year + '-' + (month - 3) + '-' + start3MonthDay; } }else{ dateObj.start = year + '-' + (month - 3) + '-' + day; } } console.log(JSON.stringify(dateObj))Copy the code