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
- var date=new date ()--Creates a Date object that represents the current time of the local time zone.
- 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.
- 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
- 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).
- ToString ()-typically returns a date and time with time zone information, usually expressed in military time (0-23).
- 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
- 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 locale-specific format.
- tolocaletimestring ()-Displays the time, minutes, and seconds in an implementation-specific format.
- 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
- GetTime ()--Returns the number of milliseconds that represent the date, the same as the value returned by the valueof () method.
- SetTime ()--set the date in milliseconds and change the entire date.
- getFullYear ()--Gets a 4-digit year.
- GetMonth ()--Returns the month in the date, 0-11.
- getUTCMonth ()--Returns the month of 0-11 in the UTC date.
- GetDate ()--Returns the number of days in the date month 1-31.
- GetDay ()--Returns the day of the week (0 is Sunday, 6 is Saturday).
- GetHours ()--the number of hours in the returned date (0-23).
- Getminutes ()--Returns the number of minutes in the date (0-59).
- Getseconds ()--Returns the number of seconds in the date (0-59).
JS Basic--date