This is the 26th day of my participation in the August More Text Challenge
We know that JS comes with many interfaces, and Date is one of them
Date is used to represent a Date and time. New Date() can be used to create a Date object. Once a Date object is created, the object inherits properties and methods from Date.prototype that get or set the object’s year, month, day, hour, minute, second, and millisecond
The JS Date object can represent the Date and time of 100 million days before and after January 1, 1970, that is, plus or minus 273785 years, which is now 2020, so JavaScript can represent the year 275755
First, let’s look at what’s in the Date constructor:
Here’s what I printed with Chrome version 83.0:
Parse (), now(), UTC()
function _Date() {
}
_Date.UTC = function () { }
_Date.now = function () { }
_Date.parse = function () {}Copy the code
The three functions are:
- Date.UTC(year,month,day,hours,minutes,seconds,ms)
Returns the number of milliseconds from January 1, 1970 to the specified date based on universal time
- Date.now()
Returns the number of milliseconds since 00:00:00UTC, January 1, 1970
- Date.parse()
Used to parse a string representing a date and return the number of milliseconds from 1970-1-1 00:00:00 to that date, or NaN if the string is unrecognized or in some cases contains an invalid date value (e.g. 2015-02-31)
What happens if you just call Date()?
Returns the date and time of the day of return
Now, how do I create a date object
Very simple, new Date()
But different parameters lead to different situations
- new Date()
Creates a date object with no parameter
- new Date(milliseconds)
The date object is created based on the number of milliseconds passed from 0 o ‘clock on January 1, 1970
- new Date(date_string)
Pass a Date format string, according to the string to create a Date object 4. The new Date (year, month, Date, / hour, minute, second, and millisecond)
Creates a date object based on the year, month, day, hour, minute, second, millisecond passed in, selectable in square brackets
Either way, the object will be created based on the date and time passed, and the object will be created based on the timestamp
Next, let’s go straight to Date’s prototype and see what common methods these objects use:
Don’t look at so many methods, commonly used I draw these:
1. GetFullYear () returns the four-digit year
2. GetMonth () returns an integer between 0 (January) and 11 (December)
3. GetDate () returns the number of days in a month (1-31)
4. GetDay () returns the day of the week (0 to 6, Sunday is 0)
5. GetHours () returns hours (0 ~ 23)
6. GetMinutes () returns minutes (0 ~ 59)
7. GetSeconds () returns the number of seconds (0 ~ 59)
8.getmilliseconds () returns milliseconds (0 ~ 999)
9. GetTime () returns the number of milliseconds from January 1, 1970, based on local time
The set method is also used to modify the object
In addition, the direct prototype of Date prototype is Object
That is the basic content of Data