Javascript:date Type Comprehensive Analysis _ Basic knowledge

Source: Internet
Author: User
Tags current time date1 locale time and date

Creates a Date object, using the new operator followed by the date's constructor.

var date = new Date ();

When a default constructor is invoked, the newly created date automatically obtains the current time and date. If you need to specify a date and time, you need to pass in the number of milliseconds to represent that date.

JavaScript provides two methods for calculating dates, the Date.parse () method receives a string parameter that represents a date, and then returns the corresponding number of date milliseconds based on that date. But the format of dates often varies by implementation and by region. DATE.UTC () also returns the number of milliseconds that represent the date, which is the year, the month based on 0 (January is 0), the day of the month, the number of hours (0 to 23), the minutes, seconds, and the number of milliseconds. The year and month two parameters are required.

var date = new Date (Date.parse ("May 1, 2016"));

GMT time January 1, 2016 0 o'clock in the morning
var date = new Date (DATE.UTC (2016,0));

GMT time May 10, 2016 21:46:30
var date1 = new Date (2016,5,10,21,46,30);

1. Methods of inheritance

tolocalestring (): Returns the date and time in the format that is compatible with the locale of the browser. The time format will contain am or PM, but will not contain time zone information

ToString (): Returns the date and time with time zone information, which is typically military time (the range of hours is 0 to 23).

valueof (): Instead of returning a string, it returns the number of milliseconds for the date. You can use comparison operators to compare

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 a number of methods for formatting dates into strings:

todatestring () Displays the week, month, day, and year in a specific implementation format

totimestring () Displays the time, minutes, seconds, and time zone in the implementation-specific format

tolocaledatestring () Displays the week, month, day, and year in a locale-specific format

tolocaletimestring () Displays the time, minutes, and seconds in the implementation-specific format

toutcstring () in the implementation-specific format complete UTC date

3. Date/Time component method

gettime (): Returns the number of milliseconds that represent a date

settime (): Set date in milliseconds

getmonth (): Returns the month of the date in which 0 represents

getday (): The day of the week in the return date (0 means Sunday, 6 means Saturday)

gethours (): Returns the number of hours in the date (0 to 23)

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

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

You can view the document as needed.

The above javascript:date type of comprehensive analysis is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling 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.