JS Basic--date

Source: Internet
Author: User
Tags date1 time zones local time locale

One, date type

The date type uses the number of milliseconds since UTC (coordinated Universal time, international coordination Hour) from midnight January 1, 1970 to the date of the change.

Second, create Date object

    1. var date=new date ()--Creates a Date object that represents the current time of the local time zone.
    2. var date=new date ("May 25,2004")-Returns the number of milliseconds, if the argument is a number, returns the time of the current number representing the number of milliseconds.
    3. ECMAScript provides two methods: (both based on the local time zone rather than the GTM).
    • Date.parse ()--receives a string representing the time, returns the corresponding number of milliseconds, and returns Nan if the passed-in string cannot represent the date.
    • DATE.UTC ()--receives a string representing time, returns the corresponding number of milliseconds, and differs from Date.parse () in that its parameters are year, month (0-11), Day (1-31), Hours (0-23), minute seconds, and number of milliseconds, where the year and month are required.
    • Date.now ()--Returns the number of milliseconds that represents the date and time the method was called.
varDate=Newdate (); Console.log (date);//Date {Fri 15:17:27 gmt+0800}varDate=NewDate ("2004/5/25");//call Date.parse () in the backgroundConsole.log (date);//Date {Tue 2004 00:00:00 gmt+0800}varDate=NewDate (Date.parse ("2004/5/25")) ); Console.log (date) ;//Date {Tue 2004 00:00:00 gmt+0800}Console.log (Date.parse ("2004/5/25"));//1085414400000varDate=NewDate (DATE.UTC (2005,04,5,17,55,55) ); Console.log (date) ;//Date {Fri 2005 01:55:55 gmt+0800}varstart=Date.now (); for(vari=0;i<10;i++){}varstop=Date.now (); result=stop-Start;console.log (result);//The 1,for Loop performs a 1ms

Iii. Methods of succession

    1. toLocaleString ()-Returns the date and time in a format appropriate to the locale of the browser, possibly including AM,PM, but does not contain time zone information (the format varies by browser).
    2. ToString ()-typically returns a date and time with time zone information, usually expressed in military time (0-23).
    3. ValueOf ()--does not return a string, but instead returns the milliseconds of the date. Facilitates time comparisons.
var date1=New Date (2007,0,1); var date2=New Date (2007,1,1); Console.log (Date1<date2); // trueconsole.log (DATE1>DATE2); // false

Iv. formatting methods of dates

    1. todatestring ()-Displays the day of the week, months, days, and years in an implementation-specific format.
    2. totimestring ()-Displays hours, minutes, seconds, and time zones in an implementation-specific format.
    3. tolocaledatestring ()-Displays the day of the week, months, days, and years in a locale-specific format.
    4. tolocaletimestring ()-Displays the time, minutes, and seconds in an implementation-specific format.
    5. toutcstring ()--The complete UTF format in the implementation-specific format.
var date=New  date (); Console.log (date.todatestring ()); // SatConsole.log (date.totimestring ()); // 23:01:04 gmt+0800console.log (date.tolocaledatestring ()); // 2016/8/6Console.log (date.tolocaletimestring ()); // afternoon 11:01:04Console.log (date.toutcstring ()); // Sat, 15:01:04 GMT

Vi. Date/Time component methods

Used in

    1. GetTime ()--Returns the number of milliseconds that represent the date, the same as the value returned by the valueof () method.
    2. SetTime ()--set the date in milliseconds and change the entire date.
    3. getFullYear ()--Gets a 4-digit year.
    4. GetMonth ()--Returns the month in the date, 0-11.
    5. getUTCMonth ()--Returns the month of 0-11 in the UTC date.
    6. GetDate ()--Returns the number of days in the date month 1-31.
    7. GetDay ()--Returns the day of the week (0 is Sunday, 6 is Saturday).
    8. GetHours ()--the number of hours in the returned date (0-23).
    9. Getminutes ()--Returns the number of minutes in the date (0-59).
    10. Getseconds ()--Returns the number of seconds in the date (0-59).

JS Basic--date

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.