The _javascript techniques of the native Js:date objects in the whole analysis

Source: Internet
Author: User
Tags current time getdate instance method iso 8601 iso 8601 format local time readable string format

Date object: Number of milliseconds from January 1, 1970 (World Standard Time)
This article refer to MDN do detailed finishing, convenient for everyone to refer to MDN

constructor function:

1.new Date (); Creates a Date object based on the current time set by the system.

2.new Date (value); Value represents the number of milliseconds that have elapsed since January 1, 1970 00:00:00 (World Standard Time).

3.new Date (datestring); DateString A String value representing the date. The string should be recognized by the Date.parse () method (in accordance with Ietf-compliant RFC 2822 timestamps or version of ISO8601)

4.new Date (year, month[, day[, hour[, minutes[, seconds[, milliseconds]]];

Year 1900 years later, an integer value representing the year. To avoid the 2000 problem, it would be preferable to designate a 4-digit year; Use 1998 instead of 98.

Month an integer between 0 and 11 that represents the month (January) to 11 (December).

An integer from day 1 through 31 that represents the days ordinal of a month.

Hour an integer between 0 and 23 that represents the hour.

Minute an integer from 0 to 59 that represents the minute.

Second an integer from 0 to 59, the number of seconds.

Millisecond an integer from 0 to 999 that represents the integer value of the millisecond portion of the time.

If at least two parameters are provided, the rest of the parameters are set to 1 by default, if the day parameter is not provided, or 0.

The JavaScript time is started by World standard Time (UTC) January 1, 1970, and is timed in milliseconds, consisting of 86,400,000 milliseconds a day. The Date object's range is 100,000,000 days to 100,000,000 days (the equivalent of a millisecond value).

The date object for JavaScript provides a unified behavior across platforms. The time attribute can represent the same time on different systems, and if the local time object is used, the local time is reflected.

The JavaScript Date object provides a method of several UTC times, and provides a local time method accordingly. UTC, which is what we call GMT, refers to the world time standard in times. Local time refers to the time that is set by the client computer that executes JavaScript.

Invoking a JavaScript Date object (without using the new operator) in the form of a function returns a string representing the current date and time. Rather than a date object. In addition, unlike other JavaScript types, the Date object has no literal formatting.

When date is invoked as a constructor and passes in multiple parameters, the adjacent values are adjusted if the value is greater than the reasonable range (for example, the month is 13 or the number of minutes is 70). For example, new Date (2013, 13, 1) equals new Date (2014, 1, 1), all of which represent the date 2014-02-01 (note that the month starts from 0). Other values are similar, new Date (2013, 2, 1, 0, 70) equals new Date (2013, 2, 1, 1, 10), all representing time 2013-03-01t01:10:00.

When date is called and passed in multiple parameters as a constructor, the defined parameter represents the local time. If world coordination is required, use the new Date ({{Jsxref ("DATE.UTC ()", "DATE.UTC (...)")}) and the same parameters

Property:

Date.prototype: Allows attributes to be added to a Date instance object.

Date.length: The value is 7. This is the number of parameters that the constructor can accept.

Date static method:

Date.now () IE9

Returns the number of milliseconds that have elapsed since 1970-1-1 00:00:00 UTC (Time Standard Time).

In browsers that support the high fineness (high-resolution) time features of the Web performance API, Window.performance.now provides more reliable and accurate time than Date.now.

Compatible with older browsers:

if (! Date.now) {
 Date.now = function Now () {return
  new Date (). GetTime ();}
 ;
}

Date.parse ()

Resolves a string that represents a date and returns the number of milliseconds elapsed since 1970-1-1 00:00:00. Returns nan if the parameter cannot be resolved to a valid date.

The parse method takes 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.

The method can accept strings that conform to the RFC2822/IETF date syntax (RFC2822 section 3.3), such as "Mon, DEC 1995 13:30:00 GMT". This method can understand the abbreviation of the continental time zone, but for more general use time zone offsets, such as "Mon 1995 13:30:00 +0430" (4 hours and 30 minutes to the east of Greenwich Meridian). 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 resolved in the local time zone.

Because deviations in the resolution of date strings result in inconsistent results, it is recommended that you always parse the date strings manually, especially when different ECMAScript implementations parse strings such as "2015-10-12 12:00:00" to NAN,UTC or local time.

In addition, the date time string can also be 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 parameters with the same parameters (from 2 to 7) as the longest form of the constructor, and returns the number of milliseconds that have elapsed since 1970-01-01 00:00:00 UTC.

You should specify a full format for the year, such as 1998

The DATE.UTC method replaces local time with the coordinated world.

The Date.utc method returns a time value, not a Date object.

If one of the specified arguments is outside its reasonable range, the UTC method updates the other parameters until the parameter is reasonable. For example, specifying 15 for a month will add 1 to the year, and then the month will use 3.

Date instance method:

Based on the method of local time

Get Time Method:

Date.prototype.getDate ()

Returns the day ordinal (1-31) of the month of the specified date object, based on the local time.

Date.prototype.getDay ()

Returns the day of the week of the specified Date object, based on the local time (0-6).

Date.prototype.getFullYear ()

Returns the year of a specified date object based on the local time, which returns a four-digit year from 1000 to 9999, using this method instead of the GetYear method.

Date.prototype.getHours ()

Returns the hour (0-23) of the specified Date object based on the local time.

Date.prototype.getMilliseconds ()

Returns the Microsecond (0-999) of the specified Date object, based on the local time.

Date.prototype.getMinutes ()

Returns the minute (0-59) of a specified Date object based on the local time.

Date.prototype.getMonth ()

Returns the month (0-11) of the specified Date object, based on the local time.

Date.prototype.getSeconds ()

Returns the number of seconds (0-59) of a specified Date object based on the local time.

Date.prototype.getTime ()

Returns the milliseconds, number type, from 1970-1-1 00:00:00 UTC (Coordinated world time) to that date, which returns a negative value for the time before 1970-1-1 00:00:00 UTC. This method has the same functionality as the valueof () method.

Date.prototype.getTimezoneOffset ()

Returns the time lag value in Coordinated Universal Time (UTC) relative to the current timezone, in minutes.

Time zone deviation (Time-zone offset) represents the difference between Coordinated Universal Time (UTC) and the local time zone, in minutes. It is worth noting that if the local time zone is later than the coordinated world, the difference is positive if it is a negative value earlier than the coordinated world

Set the time method: if one of the following parameters is outside the reasonable range, the method updates the other parameter values, and the date values of the date objects are updated accordingly

Date.prototype.setDate (value)

Sets the day ordinal of the month for the specified date object, based on the local time.

If value is outside the reasonable range of the month, setdate updates the Date object accordingly. For example, if you specify 0 for value, the date is set to the last day of the last month, and can be negative.

Date.prototype.setFullYear (yearvalue[, monthvalue[, Dayvalue])

Sets the full year for the specified date object based on the local time (four digits for the four-digit year).

Yearvalue the integer value of the specified year, for example, 1995.

Monthvalue an integer value between 0 and 11, indicating from January to December.

Dayvalue an integer value from 1 to 31 that represents the day ordinal of the month. If you specify the Dayvalue parameter, you must specify both Monthvalue.

If 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]])

Sets the number of hours for a specified date object based on the local time. If you do not specify Minutesvalue,secondsvalue and Msvalue parameters, the return value of the getminutes (), getseconds (), and getmilliseconds () methods are used.

parameter is out of bounds and automatically adjusts for updates

Date.prototype.setMilliseconds ()

Sets the number of milliseconds for a specified date object based on the local time.

Date.prototype.setMinutes ()

Sets the number of minutes for a specified date object based on the local time.

Date.prototype.setMonth ()

Sets the month for a specified date object based on the local time.

Date.prototype.setSeconds ()

Sets the number of seconds for a specified date object based on the local time.

Date.prototype.setTime (TimeValue)

Sets the time of the date object by specifying the number of milliseconds to start from 1970-1-1 00:00:00 UTC, and negative values are available for time earlier than 1970-1-1 00:00:00 UTC. Example: Samebigday.settime (Newbigday.gettime ());

The world-time standard approach:

Date.prototype.getUTCDate ()

Returns a specified Date object that is the first day of the one month, returning an integer value of 1 to 31, as the world time standard

Date.prototype.getUTCDay ()

Returns a specified Date object for the first day of the week, of which 0 represents Sunday, in the world-time standard.

Date.prototype.getUTCFullYear ()

Returns the year of a specified date object, in the world-time standard, which returns a four-digit year from 1000 to 9999

Date.prototype.getUTCHours ()

Returns the number of hours for a specified date object, in the world-time standard.

Date.prototype.getUTCMilliseconds ()

Returns the number of milliseconds for a specified date object, in the world-time standard ...

Date.prototype.getUTCMinutes ()

Returns the number of minutes for a specified date object, in the world-time standard.

Date.prototype.getUTCMonth ()

Returns the month of a specified date object, based on the world standard, which is counted from 0 (0 represents the first month of the year).

Date.prototype.getUTCSeconds ()

Returns the number of seconds for a specified date object, in the world-time standard.

Date.prototype.setUTCDate ()

Sets the day of the month in the Date object according to the world (1 ~ 31).

Date.prototype.setUTCFullYear ()

Sets the year (four digits) in the Date object based on the world.

Date.prototype.setUTCHours ()

Sets the hour in the Date object according to the world (0 ~ 23).

Date.prototype.setUTCMilliseconds ()

Sets the milliseconds in the Date object according to the world (0 ~ 999).

Date.prototype.setUTCMinutes ()

Sets the minutes in the date object according to the world (0 ~ 59).

Date.prototype.setUTCMonth ()

Sets the month in the Date object according to the world (0 ~ 11).

Date.prototype.setUTCSeconds ()

Set the seconds in the Date object according to the world (0 ~ 59)

Date format conversion:

Date.prototype.toDateString ()

A string that returns the date part of the Date object in Human readable form (human-readable).

Date.prototype.toISOString () IE9

Returns a string in ISO 8601 Extended format format: YYYY-MM-DDTHH:MM:SS.SSSZ. Time zone is always UTC (coordinated world time), plus a suffix "Z" identification

Date.prototype.toJSON ()

Returns a JSON format string (using Toisostring ()) that represents the value of the Date object. By default, this method is commonly used for JSON serialization of date objects ... In order to be used in the Json.stringify () method.

Date.prototype.toLocaleDateString ()

Returns a String representing the date part of the Date object, which is associated with a system-set locale (locality sensitive). Other same tolocaletimestring ()

Date.prototype.toLocaleString ()

Returns a String representing the date object that is associated with the locale of the system. Overrides the Object.prototype.toLocaleString () method. Other same tolocaletimestring ()

Date.prototype.toLocaleTimeString ([locales [, options]])

Returns a String representing the time part of the Date object, which is associated with a system-set locale (locality sensitive).

method returns a string for the date part of the Date object, which differs in different languages. The new parameters (IE11, and Safari none) locales and options enable the program to specify which language formatting rules to use, allowing customization of the method's performance (behavior). In older browsers, the locales and options parameters are ignored, and the language environment used and the string format returned are independently implemented

Check the browser compatibility section to see which browsers support the locales and options parameters, as well as examples: detecting locales and options parameter support.

Returns a formatted string that uses the default locale and formatting (options) when no locale (locale) is specified.

Locales and options parameters are not supported by all browsers. To detect whether an implementation environment (implementation) supports them, an illegal language tag can be used, and if the implementation environment supports that parameter, a Rangeerror exception is thrown and the argument is ignored.

Performance: When you format a large number of dates, it is a good idea to create a Intl.datetimeformat object and then use the method provided by the object's Format property.

Date.prototype.toString ()

Returns a String representing the Date object, always returning a string in the American English date format. Overrides the Object.prototype.toString () method.

Date.prototype.toTimeString ()

A string that returns the time part of a Date object in human readable format.

Date.prototype.toUTCString ()

Converts a Date object to a string that is timed in the UTC time zone.

Date.prototype.valueOf ()

Returns the number of milliseconds from January 1, 1970 0:0 0 seconds (UTC, that is, coordinated world) to the time represented by the date object. The functionality of this method, like the Date.prototype.getTime () method, overrides the Object.prototype.valueOf () method.

The above original Js:date object comprehensive analysis is a small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.