Get the time in the past weekvar end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; //0-11 means January to December var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; if (day - 7 <= 0) { //If it is before the 7th of the month var startMonthDay = new Date(year, (parseInt(month) - 1), 0).getDate(); //Total number of days in the month 1 week ago if (month - 1 <= 0) { //If it is in January of the current year dateObj.start = (year - 1) + '-' + 12 + '-' + (31 - (7 - day)); } else { dateObj.start = year + '-' + (month - 1) + '-' + (startMonthDay - (7 - day)); } } else { dateObj.start = year + '-' + month + '-' + (day - 7); } console.log(JSON.stringify(dateObj)) 1.2.3.4.5.6.7.8.9.10.11.12.13.14.15.16.17. Get the time of the past monthvar end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; //0-11 means January to December var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; var endMonthDay = new Date(year, month, 0).getDate(); //Total number of days in the current month if(month - 1 <= 0){ //If it is January, move forward one year<br> 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 in the month one month ago is less than the current date if(day < endMonthDay){ //The current date 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; } } console.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. Get the last three monthsvar end = new Date(); var year = end.getFullYear(); var month = end.getMonth() + 1; //0-11 means January to December var day = end.getDate(); var dateObj = {}; dateObj.end = year + '-' + month + '-' + day; var endMonthDay = new Date(year, month, 0).getDate(); //Total number of days in the current monthif(month - 3 <= 0){ //If it is January, February or March, move forward one year var start3MonthDay = new Date((year - 1), (12 - (3 - parseInt(month))), 0).getDate(); //Total number of days in the month 3 months ago if(start3MonthDay < day){ //The total number of days in the month 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 in the month 3 months ago if(start3MonthDay < day){ //The total number of days in the month 3 months ago is less than the current date if(day < endMonthDay){ //The current date is less than the total number of days in the current month, February is a special month 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)) New Date() vs. setDate() ParametersI believe there are already many articles about dates on the Internet. Here I just summarize the problems I encountered at work. new Date() new Date() has six forms, five with parameters and one without parameters;
Parameter Description: month1: In English, it indicates the name of the month; from January to December; dd: indicates date, 1-31 yyyy: indicates the year represented by four digits hh:mm:ss: indicates time, hour (0-23) - minute (0-59) - second (0-59) month2: is a number type month; from 0 to 11; i.e. January to December ms: The number of milliseconds since January 1, 1970 Special reminder: some are in character form and some are not SummarizeThis is the end of this article about how to use js to get the time of the past week, month, and three months. For more related content about how to use js to get the time of a week, month, and three months, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Implementation of waterfall layout + dynamic rendering
>>: MySQL Database Indexes and Transactions
This article records the detailed tutorial of MyS...
Table of contents background Solution New Questio...
Table of contents Preface What is DrawCall How do...
Many times we want the server to run a script reg...
MySQL 5.7 adds many new features, such as: Online...
1. Demand The base has 300 new servers, and needs...
Development Background: Recently, I am working on...
1. Fixed width + adaptive Expected effect: fixed ...
Preface Histogram is a basic statistical informat...
Some special characters and icons used in the pro...
Table of contents 1. DOM Diff 2. Add key attribut...
This article shares the specific code for JavaScr...
Effect picture: Implementation code: <template...
Table of contents Installation Prerequisites Step...
Table of contents Axios Request Qs processing dat...