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
Port Mapping Before the Docker container is start...
Table of contents Overview Defining filters Use o...
The first tutorial for installing MySQL-5.7.19 ve...
Recently I saw an article on a public account tha...
Preface As you know, Linux supports many file sys...
This article shares the specific code of NodeJS t...
1. First find the Data file on the migration serv...
Add monitoring host Host 192.168.179.104 is added...
When it comes to understanding web design, many p...
conda update conda pip install tf-nightly-gpu-2.0...
MySQL installation tutorial, for your reference, ...
This article shares the specific code for the WeC...
Table of contents 1. Basic Use 2. Image quantity ...
Table of contents 1. Brief Overview 2. Detailed e...
In the interview, you should have experienced the...