Built-in object Date
Date object is also used very frequently in actual development. It is a built-in object used to represent dates and times.
Date is an object used to process dates and times, but unlike Math objects, Date is a constructor that needs to be instantiated before it can be used.
Next first step: Create a Date object
Create a Date object
There are two ways to create a Date object:
-
No parameters are passed when creating:
Without passing parameters, the final result is to return the current time object of the systemvar date = new Date(); (date); // Wed Sep 28 2022 18:06:49 GMT+0800 (China Standard Time) (typeof date); // object
Pass parameters when creating:
When passing parameters, an object that creates a specified time. The parameters can be a string, a number, or a timestamp. We explain it separately according to the type of parameters passed.
-
The parameter is a string
var date = new Date("2022-09-28 18:06:49"); var date = new Date("2022/09/28 18:06:49"); var date = new Date("2022-09-28T18:06:49"); var date = new Date("2022/09/28T18:06:49"); var date = new Date("2022-09-28"); var date = new Date("2022/09/28"); var date = new Date("2022-09-28T00:00:00"); var date = new Date("2022/09/28T00:00:00");
Through the above code, we can see that when passing the time string as a parameter, the format we need to follow is: year, month and day. The specific time, where the year, month and day are used between year, month and day.-
or/
Connect, use the specific time:
Connection; connections between year, month, day and specific time can be usedSpaces
Separate orT
Separate.
-
Parameters are multiple numbers
var date = new Date(x,y,z,a,b,c,d);
- x: indicates the year, must be passed, otherwise an error will be reported
- y: represents month, starting from 0, 0 represents January, 1 represents February, and so on. If not passed, the default is 0
- z: Indicates the date, if not passed, the default is 1
- a: means hour, if not passed, the default is 0
- b: Indicates minutes, if not passed, the default is 0
- c: means seconds, if not passed, the default is 0
- d: means milliseconds, if not passed, the default is 0
We use comma intervals between multiple parameters. If the parameters are not passed, the default value will be used (except for the year, which must be passed in the year).
-
The parameter is a timestamp
var date = new Date(timestamp);
Format of dates
In actual development, we often need to format the date, for example: format the date to: 2022-09-28 18:06:49. At this time, we need to use the format method of the Date object.
Or we need to get the specified part of the date, and at this time we need to use the method provided by the Date object.
Methods that come with Date objects
Method name | meaning | Remark |
---|---|---|
getFullYear() | Get Year | |
getMonth() | Get Month: 0-11 | 0 represents January |
getDate() | Acquisition date: 1-31 | |
getDay() | Get Week: 0-6 | 0 represents Sunday; 1 represents Monday |
getHours() | Get hours: 0-23 | |
getMinutes() | Get minutes: 0-59 | |
getSeconds() | Get seconds: 0-59 | |
getMilliseconds() | Get milliseconds | 1s = 1000ms |
Get the timestamp
What is a timestamp?
The timestamp is the number of milliseconds taken from January 1, 1970, GMT to the current date (1 second = 1000 milliseconds).
Then why do you need a timestamp?
This is because in computers, time is stored in numbers, not in strings, so we need to convert time into numbers, which is the timestamp.
The existence of timestamps is to unify the unit of time.
How to get the timestamp?
The following common methods are:
-
+new Date(): Get the time stamp of the current time
value = +new Date();
-
getTime method of Date object
var date = new Date();
-
valueOf method of Date object
var date = new Date();
All three methods can get the timestamp of the current time. The frequency of use decreases from top to next.
Appendix: Get the total number of milliseconds of Date (how many milliseconds have passed since January 1, 1970)
valueOf();
var date = new Date(); (());//1628750819687
getTime();
var date = new Date(); (());//1628750819687
Simple writing
var date1 = +new Date(); (date1);//1628750819687
New methods for H5
(());//1628750819687
Summarize
This is the end of this article about some common methods of Date in JS. For more information about common methods of JS, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!