The date type in JavaScript

Source: Internet
Author: User
Tags time zones iso 8601 local time month name

The date type in ECMAScript is built on the Java.util.Date class in early java. To do this, the date type uses the number of milliseconds since UTC (coordinated Universal time, international coordination hour) to start January 1, 1970 0 o'clock to save the dates. With this data storage format, the date type can be saved for exactly 100 million years before or after January 1, 1970.

To create a Date object, use the new action to conform to the date constructor.

var New Date ();

The newly created object automatically gets the current date and time in the event that the date constructor is called without parameters. If you want to create a Date object based on a specific date and time, you must pass in the number of milliseconds that represent that date (the number of milliseconds from January 1, 1970 0 o'clock to that date).

  

ECMAScript provides two methods: Date.parse () and DATE.UTC ().

  Date.parse ()

  The Date.parse () method receives a string parameter that represents a date, and then attempts to return the number of milliseconds of the corresponding date based on the string. ECMA-262 does not define which date format the Date.parse () should support, so the behavior of this method varies by implementation. Browsers that set the region to the United States generally accept the following date formats:

? "Month/day/year", as in 9/7/2017;

? "English month name day, year", such as January 12,2004;

? "English days of the English month: minutes: seconds time zone", such as Thur September 7 00:00:00 GMT-0700.

? ISO 8601 extended format yyyy-mm-ddthh:mm:ss.sssz (e.g. 2017-09-07t00:00:00). Only implementations that are compatible with ECMAScript 5 support this format.

For example, to create a Date object for September 7, 2017, you can use the following code:

var New Date (Date.parse ("September 7,2017"));

If the string passed into the Date.parse () method cannot represent a date, it returns Nan. In fact, if a string representing a date is passed directly to the date constructor, Date.parse () is also called in the background. For example:

var New Date ("September 7,2017");

  

  DATE.UTC ()

The DATE.UTC () method also returns the number of milliseconds that represents a date, but it uses different information with date.parse () when building values. The arguments for DATE.UTC () are the year, the month based on 0 (January is 0, February is 1), the Day of the month (1 to 31), the number of hours (0 to 23), the minute, the second, and the number of milliseconds. Of these parameters, only the first two parameters (year and month) are required. If the number of days in the month is not provided, the number of days is assumed to be 1, and if other parameters are omitted, all are assumed to be 0.

// GMT Time January 1, 2000 Midnight 0 O'Clock var New Date (DATE.UTC (2000,0)); // GMT May 5, 2005 PM 5:55:55 var New Date (DATE.UTC (2005,4,5,17,55,55));

Like Date.parse (), the date constructor mimics Date.utc (), but it is a little different; the date and time are created based on the local time zone instead of GMT.

ECMAScript 5 adds the Date.now () method, which returns the number of milliseconds that represent the date and time when this method was called. This approach simplifies the work of parsing code using date objects.

// Get start time var start = date.now (); // calling Functions dosomething (); // Get stop Time var stop = Date.now (), result = Stop-start;

Browsers that support the Date.now () method include ie9+, Firefox, Safari, Opera 10.5, and Chrome. In browsers that do not support it, using the + operator to get the timestamp of a Date object can also achieve the same purpose.

// Get start time var start = +new  Date (); // calling Functions dosomething (); // Get stop Time var stop = +new Date (), result = Stop-start;

  Method of date formatting

The date type also has some methods that are specifically used to format dates as strings, as follows:

? toDateString ()-Displays the day of the week, months, days, and years in an implementation-specific format;

? toTimeString ()-Displays hours, minutes, seconds, and time zones in an implementation-specific format;

? toLocaleDateString ()-Displays the day of the week, months, days, and years in a region-specific format;

? toLocaleTimeString ()-Displays the time, minutes, and seconds in an implementation-specific format;

? toUTCString ()--full UTC date In a format that is specific to the implementation.

  Method of the date type

? GetTime (): Returns the number of milliseconds representing the date, the same as the value returned by the valueof () method

? SetTime (MS): Set the date in one millisecond and change the entire date

? getFullYear (): 4-digit year

? getUTCFullYear (): Returns the 4-digit year of the URC date

? setFullYear (year): Sets the year of the date. The incoming year value must be a 4-digit number.

? setUTCFullYear (year): Sets the year of the UTC date. The incoming year value must be a 4-digit number.

? GetMonth (): Returns the month of the date, where 0 represents January, and 11 represents December.

? getUTCMonth (): Returns the month of the UTC date, where 0 represents January, and 11 represents December.

? Setmonth (Month): Sets the month of the date. The incoming month value must be greater than 0, and more than 11 increases the year

? setUTCMonth (Month): Sets the month of the UTC date. The incoming month value must be greater than 0, and more than 11 increases the year

? GetDate (): Returns the number of days in a date month (1 to 31)

? getUTCDate (): Returns the number of days in the UTC date month (1 to 31)

? SetDate (Day): Sets the number of days in the date month. If the value passed in exceeds the number of days that should be in that month, the month is incremented.

? SetUTCDate (Day): Sets the number of days in the UTC date month. If the value passed in exceeds the number of days that should be in that month, the month is incremented.

? GetDay (): Returns the day of the week (where 0 represents Sunday, 6 means Saturday)

? GetUTCDay (): Returns the day of the week in UTC (where 0 means Sunday, 6 for Saturday)

? GetHours (): Returns the number of hours in a date (0 to 23)

? getUTCHours (): Returns the number of hours in UTC date (0 to 23)

? Sethours (Time): Sets the number of hours in the date. Increase the number of days in the month if the value passed in exceeds 23

? setUTCHours (Time): Sets the number of hours in the UTC date. Increase the number of days in the month if the value passed in exceeds 23

? Getminutes (): Returns the number of minutes in a date (0 to 59)

? getUTCMinutes (): Returns the number of minutes in UTC date (0 to 59)

? Setminutes (min): Sets the number of minutes in a date. Increase the number of hours when the value passed in exceeds 59

? setUTCMinutes (min): Sets the number of minutes in UTC date. Increase the number of hours when the value passed in exceeds 59

? Getseconds (): Returns the number of seconds in a date (0 to 59)

? getUTCSeconds (): Returns the number of seconds in UTC date (0 to 59)

? Setseconds (seconds): Sets the number of seconds in the date. Increase the number of minutes if the value passed in exceeds 59

? setUTCSeconds (seconds): Sets the number of seconds in UTC date. Increase the number of minutes if the value passed in exceeds 59

? Getmilliseconds (): Returns the number of milliseconds in a date

? getUTCMilliseconds (): Returns the number of milliseconds in a UTC date

? Setmilliseconds (MS): Set the number of milliseconds in a date

? setUTCMilliseconds (milliseconds): Sets the number of milliseconds in UTC date

? getTimezoneOffset (): Returns the number of minutes between local time and UTC time. For example, US Eastern Standard Time returns 300; China standard Time returns-480.

The date type in JavaScript

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.