Date object: based on the number of milliseconds from January 1, 1970 (UNF)
This article refers to the detailed compilation of MDN, so that everyone can refer to MDN.
Constructor:
1.new Date(); Create a Date object based on the current time set by the system.
2.new Date(value); value represents the number of milliseconds elapsed from 00:00:00 (United States Standard Time) on January 1, 1970.
3.new Date(dateString); dateString represents the string value of the date. This string should be recognized by the Date.parse() method (compliant with IETF-compliant RFC 2822 timestamps or version of ISO8601)
4.new Date(year, month[, day[, hour[, minutes[, seconds[, millionseconds[, millionseconds]]]]]);
year A certain year after 1900 represents the integer value of the year. To avoid the 2000 problem, it is best to specify a 4-digit year; use 1998 instead of 98.
An integer between month 0 and 11, representing the month (January) to November (December).
A integer between 1 and 31 indicates the day of a month.
An integer between hour 0 and 23, indicating the hour.
minute An integer between 0 and 59, representing minutes. .
second An integer between 0 and 59, seconds.
millisecond An integer between 0 and 999, representing the integer value of the millisecond part of the time.
If at least two parameters are provided, the remaining parameters will be set to 1 by default (if no day parameters are provided) or 0.
JavaScript's time is started on January 1, 1970, timed in milliseconds, and a day consists of 86,400,000 milliseconds. The range of the Date object is -100,000,000 days to 100,000,000 days (the equivalent millisecond value).
JavaScript's Date object provides unified behavior for cross-platform. The time attribute can represent the same moment in different systems, and if a local time object is used, it reflects the local time.
JavaScript's Date object provides several UTC time methods, and also provides local time methods accordingly. UTC, which is what we call Greenwich time, refers to the world time standard in time. Local time refers to the time set by the client computer that executes JavaScript.
Calling JavaScript's Date object (without using the new operator) in the form of a function returns a string representing the current date and time. Instead of a date object. Also, unlike other JavaScript types, Date objects do not have a literal format.
When Date is called as a constructor and multiple parameters are passed in, if the value is greater than a reasonable range (such as 13 months or 70 minutes), the adjacent values will be adjusted. For example, new Date(2013, 13, 1) is equal to new Date(2014, 1, 1), and they all represent dates 2014-02-01 (note that the month starts from 0). Other values are similar. New Date(2013, 2, 1, 0, 70) is equal to new Date(2013, 2, 1, 1, 10), and both represent the time 2013-03-01T01:10:00.
When Date is called as a constructor and multiple parameters are passed in, the defined parameters represent the local time. If world coordination is required, use new Date({{jsxref("Date.UTC()", "Date.UTC(...)")}}) and the same parameters
property:
Date.prototype: Allows to add properties to Date instance objects.
Date.length: The value is 7. This is the number of parameters acceptable to this constructor.
Date static method:
Date.now() IE9
Returns the number of milliseconds elapsed since 1970-1-1 00:00:00 UTC (time standard time) and is type Number.
In browsers that support the high-resolution time feature of the Web Performance API, the elapsed time provided by window.performance.now is more reliable and accurate than Date.now.
Compatible with older browsers:
if (!Date.now) { Date.now = function now() { return new Date().getTime(); };}Date.parse()
Parses a string representing the date and returns the number of milliseconds elapsed from 1970-1-1 00:00:00. If the parameter cannot be parsed to a valid date, NaN is returned.
The parse method accepts a date string (for example "Dec 25, 1995") and returns the number of milliseconds from 1970-1-1 00:00:00 UTC to the date represented by the date string. This method is useful when setting date values based on string values, such as using the setTime() method and the Date() constructor.
This method can accept strings that comply with the RFC2822 / IETF date syntax (RFC2822 Section 3.3), such as "Mon, 25 Dec 1995 13:30:00 GMT". This method can understand the abbreviation of the mainland US time zone, but for more general purpose, time zone offsets should be used, such as "Mon, 25 Dec 1995 13:30:00 +0430" (Greenwich's meridian is shifted eastward by 4 hours and 30 minutes). If no time zone is specified, the local time zone is used by default.
GMT and UTC are considered equal. If the RFC2822 Section 3.3 format does not contain time zone information, the date string is parsed with the local time zone.
Since there are deviations when parsing date strings, it is recommended to always parse date strings manually, especially different ECMAScript implementations will parse strings such as "2015-10-12 12:00:00" into NaN, UTC or local time.
In addition, date and time strings can also be used in ISO 8601 format. For example, "2011-10-10" (date only) or "2011-10-10T14:48:00" (date and time) can be passed and parsed as parameters
Date.UTC()
Accepts the same argument as the longest form of the constructor (from 2 to 7) and returns the number of milliseconds elapsed from 1970-01-01 00:00:00 UTC, of type Number.
You should specify a full format year, such as 1998
The Date.UTC method uses the coordinated world era to replace local time.
The Date.UTC method returns a time value, not a date object.
If there is a specified parameter that is outside its reasonable range, the UTC method will update the other parameters until the parameter is within the reasonable range. For example, specify 15 for the month, the year will be added 1, and the month will be used 3.
Date instance method:
Method according to local time
How to get time:
Date.prototype.getDate()
Returns the day of the month (1-31) of the specified date object according to local time.
Date.prototype.getDay()
Returns the day of the week (0-6) of the specified date object according to local time.
Date.prototype.getFullYear()
Returns the year of the specified date object based on local time. This method returns a four-digit year from 1000 to 9999. Use this method instead of the getYear method.
Date.prototype.getHours()
Returns the hour (0-23) of the specified date object according to local time.
Date.prototype.getMilliseconds()
Returns microseconds (0-999) of the specified date object according to local time.
Date.prototype.getMinutes()
Returns the minutes of the specified date object based on local time (0-59).
Date.prototype.getMonth()
Returns the month (0-11) of the specified date object according to local time.
Date.prototype.getSeconds()
Returns the number of seconds (0-59) of the specified date object according to local time.
Date.prototype.getTime()
Returns the number of milliseconds elapsed from 1970-1-1 00:00:00 UTC (Coordinated Universal Time) to that date, the Number type, and returns a negative value for the time before 1970-1-1 00:00:00 UTC. This method has the same function as the valueOf() method.
Date.prototype.getTimezoneOffset()
Returns the time difference between the coordinated Universal Time (UTC) relative to the current time zone in minutes.
Time-zone offset represents the difference between coordinated Universal Time (UTC) and the local time zone in minutes. It should be noted that if the local time zone is later than the coordinated world time, the difference is positive, and if it is earlier than the coordinated world time, the difference is negative.
Set time method: If one parameter exceeds the reasonable range, the method will update other parameter values, and the date value of the date object will also be updated accordingly.
Date.prototype.setDate(value)
Sets the day of the month for the specified date object based on the local time.
If the value exceeds the reasonable range of the month, setDate will update the Date object accordingly. For example, if 0 is specified for value, the date will be set to the last day of the previous month, which can be a negative number.
Date.prototype.setFullYear(yearValue[, monthValue[, dayValue]])
Set the full year for the specified date object based on local time (the four-digit year is four numbers).
yearValue specifies the integer value of the year, such as 1995.
monthValue An integer value between 0 and 11, indicating from January to December.
dayValue An integer value between 1 and 31 indicates what day of the month. If you specify the dayValue parameter, you must also specify monthValue.
If the monthValue and dayValue parameters are not specified, the return value of the getMonth and getDate methods will be used.
Date.prototype.setHours(hoursValue[, minutesValue[, secondsValue[, msValue]]])
Set the number of hours for the specified date object based on the local time. If the minutesValue, secondsValue and msValue parameters are not specified, the return values of the getMinutes(), getSeconds(), and getMilliseconds() methods are used.
Parameters exceed reasonable range, automatic adjustment and update
Date.prototype.setMilliseconds()
Set the number of milliseconds for the specified date object based on the local time.
Date.prototype.setMinutes()
Set the number of minutes for the specified date object based on the local time.
Date.prototype.setMonth()
Set a month for the specified date object based on the local time.
Date.prototype.setSeconds()
Set the number of seconds for the specified date object based on the local time.
Date.prototype.setTime(timeValue)
Set the time of the date object by specifying the number of milliseconds elapsed from 1970-1-1 00:00:00 UTC, and a negative value can be used for times earlier than 1970-1-1 00:00:00 UTC. Example: sameBigDay.setTime(newBigDay.getTime());
A standard method based on world time:
Date.prototype.getUTCDate()
Based on world time as the standard, return a specified date object that day of the month, returning an integer value of 1 to 31.
Date.prototype.getUTCDay()
Based on world time, return a specified date object as the day of the week, where 0 represents Sunday.
Date.prototype.getUTCFullYear()
Based on world time, it returns the year of a specified date object. This method returns a four-digit year from 1000 to 9999.
Date.prototype.getUTCHours()
Based on world time, returns the number of hours of a specified date object.
Date.prototype.getUTCMilliseconds()
Based on world time, returns the number of milliseconds of a specified date object. .
Date.prototype.getUTCMinutes()
Based on world time, returns the number of minutes of a specified date object.
Date.prototype.getUTCMonth()
Based on world time, returns the month of a specified date object, which is counted from 0 (0 represents the first month of the year).
Date.prototype.getUTCSeconds()
Based on world time, returns the number of seconds for a specified date object.
Date.prototype.setUTCDate()
Set the day of the month in the Date object according to the world time (1 ~ 31).
Date.prototype.setUTCFullYear()
Set the year (four digits) in the Date object according to world time.
Date.prototype.setUTCHours()
Set the hours (0 ~ 23) in the Date object according to the world time.
Date.prototype.setUTCMilliseconds()
Set milliseconds in the Date object according to world time (0 ~ 999).
Date.prototype.setUTCMinutes()
Set the minutes in the Date object according to the world time (0 ~ 59).
Date.prototype.setUTCMonth()
Set the month (0 ~ 11) in the Date object according to the world time.
Date.prototype.setUTCSeconds()
Set the seconds in the Date object according to the world time (0 ~ 59)
Date format conversion:
Date.prototype.toDateString()
Returns a string of the date part of the date object in human-readable form.
Date.prototype.toISOString() IE9
Returns a string in ISO (ISO 8601 Extended Format) format: YYYY-MM-DDTHH:mm:ss.ssZ. The time zone is always UTC (coordinated Universal Time), with the suffix "Z" to identify it
Date.prototype.toJSON()
Returns a JSON format string (using toISOString()) representing the value of the date object. By default, this method is often used to serialize Date objects by JSON. . For use in the JSON.stringify() method.
Date.prototype.toLocaleDateString()
Returns a string representing the date part of the date object, and the string format is associated with the locality sensitive set by the system. Others same as toLocaleTimeString()
Date.prototype.toLocaleString()
Returns a string representing the date object, which is associated with the region set by the system. Overrides the Object.prototype.toLocaleString() method. Others same as toLocaleTimeString()
Date.prototype.toLocaleTimeString([locales [, options]])
Returns a string representing the time part of the date object, and the string format is associated with the locality sensitive set by the system.
Method returns a string for the date part of the date object, and the format of the string varies from language to language. New parameters (IE11, but Safari does not) locales and options enable the program to specify which language formatting rules to use, allowing customization of the method's behavior. In older browsers, locales and options parameters were ignored, and the locale used and the returned string formats were implemented independently.
Check the browser compatibility section to see which browsers support locales and options parameters. You can also refer to the example: Detect the support of locales and options parameters.
When no locale is specified, a formatted string using the default locale and formatting settings (options).
The locales and options parameters are not supported by all browsers. To detect whether an implementation environment supports them, an illegal language tag can be used. If the implementation environment supports this parameter, a RangeError exception will be thrown, otherwise the parameter will be ignored.
Performance: When formatting a large number of dates, it is best to create an Intl.DateTimeFormat object and then use the method provided by the format property of the object.
Date.prototype.toString()
Returns a string representing the date object, always returning a string in American English date format. Overrides the Object.prototype.toString() method.
Date.prototype.toTimeString()
Returns a string of the time part of the date object in human-readable format.
Date.prototype.toUTCString()
Converts a date object to a string timed in the UTC time zone.
Date.prototype.valueOf()
Returns the number of milliseconds from 0:00:00 on January 1, 1970 (UTC, i.e. Coordinated Universal Time) to the time represented by the object on that date. The function of this method is the same as the Date.prototype.getTime() method, overriding the Object.prototype.valueOf() method.
The above comprehensive analysis of native JS:Date objects is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.