JavaScript: Full parsing of the Date type and full parsing of the date type

Source: Internet
Author: User

JavaScript: Full parsing of the Date type and full parsing of the date type

Create a Date object using the new operator followed by the Date constructor.

Var date = new Date ();

When the default constructor is called, the new date automatically obtains the current time and date. If you need to specify the date and time, you need to input the number of milliseconds that indicate the date.

JavaScript provides two methods to calculate the Date. The Date. parse () method receives a string parameter that represents the Date, and then returns the corresponding Date in milliseconds Based on the Date. However, the date format often varies by implementation and region. Date. UTC () also returns the number of milliseconds that indicate the date. The parameters are respectively the Year, the month based on 0 (January is 0), the day of the month, the hour (0 to 23) the number of minutes, seconds, and milliseconds. The year and month parameters are required.

Var date = new Date (Date. parse ("May 1, 2016"); // var date = new Date (Date. UTC (); // GMT time May 10, 2016 21: 46: 30var date1 = new Date );

1. inherited methods

• ToLocaleString (): returns the date and time in a format that is appropriate to the region set by the browser. The time format contains AM or PM, but does not contain time zone information.

• ToString (): returns the date and time with the time zone information. The time is generally the military time (the hour range is 0 to 23 ).

• ValueOf (): returns the number of milliseconds of the date instead of the string. Comparison Using comparison Operators

Var date1 = new Date (2016, 1, 9); var date2 = new Date (2016, 5, 10); alert (date1 <date2); // true

2. Date formatting Method

The Date type provides some methods to format a Date as a string:

• ToDateString () displays the day of the week, month, day, and year in a specific format.

• ToTimeString () displays the time, minute, second, and time zone in a specific format.

• ToLocaleDateString () displays the day of the week, month, day, and year in the region-specific format.

• ToLocaleTimeString () displays the time, minute, and second in a specific format.

• ToUTCString () is a fully-implemented UTC date in a specific format

3. date/time component method

• GetTime (): returns the number of milliseconds of the date.

• SetTime (): set the date in milliseconds

• GetMonth (): returns the month in the date, where 0 represents January

• GetDay (): returns the day of the week in the date (0 indicates Sunday, 6 indicates Saturday)

• GetHours (): the number of hours in the returned date (0 to 23)

• GetMinutes (): returns the number of minutes in a date (0 to 59)

• GetSeconds (): the number of seconds in the returned date (0 to 59)

You can view the document as needed.

The above JavaScript: The full parsing of the Date type is all the content shared by the editor. I hope to give you a reference and support for the help house.

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.