A simple example of using js to get the time of the last week, month and three months

A simple example of using js to get the time of the last week, month and three months

Get the time in the past week

var 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 month

var 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 months

var 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() Parameters

I 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;

  1. new Date(); Needless to say, the default date obtained is the current date.
  2. new Date("month1 dd,yyyy hh:mm:ss"); Note: The parameter is in character format
  3. new Date("month1 dd,yyyy"); Note: The parameter is in character form
  4. new Date(yyyy,month2,dd,hh,mm,ss); Note: The parameter is not a character
  5. new Date(yyyy,month2,dd); Note: The parameter is not a character
  6. new Date(ms);

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

Summarize

This 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:
  • js to get the time (this week, this quarter, this month...)
  • js implements the method of obtaining the current time and the number of weeks of this month
  • js gets the time code for today, this week, and this month
  • JS gets this week's Monday, weekend and get any time Monday weekend function example
  • js to get the time difference and remove Saturday and Sunday implementation code

<<:  Implementation of waterfall layout + dynamic rendering

>>:  MySQL Database Indexes and Transactions

Recommend

Docker primary network port mapping configuration

Port Mapping Before the Docker container is start...

How to use vue filter

Table of contents Overview Defining filters Use o...

MySQL 5.7.19 (tar.gz) installation graphic tutorial under Linux

The first tutorial for installing MySQL-5.7.19 ve...

Solution to nginx-ingress-controller log persistence solution

Recently I saw an article on a public account tha...

How to view the type of mounted file system in Linux

Preface As you know, Linux supports many file sys...

NodeJS realizes image text segmentation

This article shares the specific code of NodeJS t...

Detailed explanation of how to migrate a MySQL database to another machine

1. First find the Data file on the migration serv...

Detailed steps for adding hosts you need to monitor in zabbix

Add monitoring host Host 192.168.179.104 is added...

How to design a web page? How to create a web page?

When it comes to understanding web design, many p...

Solve the Linux Tensorflow2.0 installation problem

conda update conda pip install tf-nightly-gpu-2.0...

MySQL installation tutorial under Centos7

MySQL installation tutorial, for your reference, ...

WeChat applet implements simple calculator function

This article shares the specific code for the WeC...

Vue uses the Element el-upload component to step on the pit

Table of contents 1. Basic Use 2. Image quantity ...

Detailed explanation of MySql data type tutorial examples

Table of contents 1. Brief Overview 2. Detailed e...

What to do if the auto-increment primary key in MySQL is used up

In the interview, you should have experienced the...