Date type of the JavaScript reference type

Source: Internet
Author: User
Tags month name

The date type uses the number of milliseconds since UTC (International coordination Time) January 1, 1970 midnight (0 o'clock) to save the dates. To create a Date object, use the new operator and the date constructor:

var now = new Date ();

When you call the date constructor without passing parameters, the newly created object automatically obtains the current date and time. If you want to create a time object based on a specific date and time, you must pass in the number of milliseconds that represents that date. To simplify this computational process, JavaScript provides two methods: Date.parse () and DATE.UTC ().

where 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 and usually varies by region. Browsers that set the region to the United States generally accept the following date formats:

    • "Month/day/year", as in 6/13/2004

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

    • "English Day of the week English month name: minutes: Seconds time zone", such as Tue 2004 00:00:00 GMT-0700

For example, to create a Date object for May 25, 2004, you can use the following code:

var somedate = new Date (Date.parse ("may 25,2004"));

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. In other words, the following code is equivalent to the previous example:

var somedate = new Date ("may 25,2004");

The DATE.UTC () method also returns the number of milliseconds that represents the date, and the Date.parse () uses different information when constructing the value. The arguments for DATE.UTC () are the year, based on the month of 0 (January is 0, February is 1, and so on), the Day of the month (1~31), the number of hours (0~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, it is assumed that the number of days is 1. If other parameters are omitted, all assumptions are 0:

GMT January 1, 2000 midnight 0 o'clock var y2k = new Date (DATE.UTC (2000,0))//gmt time May 5, 2005 PM 5:55:55var allfives = new Date (DATE.UTC ( 2005,4,5,17,55,55));

As with Date.parse (), the date constructor mimics Date.utc (), but one thing is distinctly different: the date and time are created based on the local time zone rather than GMT. However, the date constructor receives the same parameters as DATE.UTC (). Therefore, if the first argument is a numeric value, the date constructor assumes that the value is the year in the date, and the second argument is the month, and so on:

local time January 1, 2000 midnight 0 o'clock var y2k = new Date (2000,0);//local time May 5, 2005 PM 5:55:55var allfives = new Date (2005,4,5,17,55,55);

Date Format method, date/Time component method and so on, don't write here, read the document !!!!!

Date type of the JavaScript reference type

Related Article

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.