JavaScript type system-Date Object

Source: Internet
Author: User
Tags date1 instance method local time time and date tojson

Previous words

The Date object is a data type built into the JavaScript language that provides an operational interface for the date and time. The Date object was created on the basis of the Java.util.Date class in early Java, and for this reason, the day type uses the number of milliseconds since the January 1 0 o'clock of UTC1970 year to save the dates, which can represent a time range of 100 million days around 0 o'clock January 1, 1970. This article describes in detail the use of the Date object

Static methods

The static method is introduced before the constructor of the date object is introduced. Because the static method of the date object is inextricably linked to its constructor. The process of creating a Date object using a constructor, similar to the use of a static method that wears a coat

The Date object has a total of three static methods, namely Date.now (), Date.parse (), and DATE.UTC (). These methods are called through the date () constructor itself, rather than through the date instance object

Date.now ()

ECMAScript5 adds the Now () method, which returns the number of milliseconds in the current time from January 1, 1970 0 o'clock UTC. The method does not support passing parameters

[Note] This method returns the number type

Console.log (Date.now ());//1468297046050console.log (Date.now (' 2016,1,1 '));//1468297046050console.log (typeof Date.now ());//' number '

In browsers that do not support the Date.now () method, you can use the + operator to convert a Date object to a number, or to achieve a similar effect

Console.log (new Date ());//tue Jul 12:21:33 gmt+0800 (China Standard Time) Console.log (+new date ());// 1468297293433console.log (+new Date (2000,1,1));//949334400000

This method is often used to analyze the work of code

var start = Date.now ();d osomething (), var stop = Date.now (); result = Stop-start;

Date.parse ()

This method is used to parse a date string, which is a string containing the date and time to parse, returning the number of milliseconds from 0 o'clock January 1, 1970 to a given date

This method parses the format of a string based on a datetime string format rule, which is supported in the following format, in addition to the standard format. If the string is not recognized, Nan is returned

1, ' Month/day/year ' such as 6/13/2004

2, ' Month day, year ' such as January 12,2004 or Jan 12,2004

3, ' Day of the month: minutes: seconds time Zone ' Tue May 2004 00:00:00 GMT-0700

[note] The browser does not support a string format that does not represent a date only time

Console.log (Date.parse (' 6/13/2004 '));//1087056000000console.log (Date.parse (' January 12,2004 ');// 1073836800000console.log (Date.parse (' Tue May 2004 00:00:00 GMT-0700 ');//1085468400000console.log (' Date.parse (' 2004-05-25t00:00:00 ')//1085443200000console.log (Date.parse ('));//1451606400000console.log (Date.parse (' T00:00:00 ');//nanconsole.log (Date.parse ());//nan

[note] In ECMAScript5, if you use a standard datetime string format rule in a string that has a pre-0 before math, it resolves to UTC time and the time does not have a predecessor of 0, then resolves to local time. Other situations usually resolve to local time

Console.log (Date.parse (' 7/12/2016 '));//1468252800000console.log (Date.parse (' 2016-7-12 '));// 1468252800000console.log (Date.parse (' 2016-07-12 '));//1468281600000

DATE.UTC ()

DATE.UTC () also returns the number of milliseconds for a given date, but its argument is not a string, but a numeric parameter that represents the year, month, day, time, minute, second, and millisecond, respectively.

DATE.UTC (YEAR,MONTH,DAY,HOURS,MINUTES,SECONDS,MS), the year and month parameters are fixed, the remaining parameters are optional, and the DateTime format rules are described in this

Because the function has 7 parameters, it has a length value of 7

Console.log (Date.UTC.length);//7

[Note] This method uses UTC time instead of local time

Console.log (DATE.UTC (1970));//nanconsole.log (DATE.UTC (1970,0));//0console.log (DATE.UTC (1970,0,2));// 86400000console.log (DATE.UTC (1970,0,1,1));//3600000console.log (DATE.UTC (1970,0,1,1,59));//714000console.log ( DATE.UTC (1970,0,1,1,59,30));//717000

constructor function

The Date () constructor has up to 5 ways to use

The "0" Date () function can be called without the new operator, like a function. It ignores all incoming arguments and returns a string representation of the current date and time

    Date ();

[note] because the date () function does not use an operator, it cannot actually be called a constructor

Console.log (date ());//"Tue Jul 13:38:41 gmt+0800 (China Standard Time)" Console.log (Date (' 2016/1/1 '));//"Tue Jul 12 2016 13:38 : gmt+0800 (China Standard Time) "Console.log (typeof date ());//' String '

The "1" date () function uses the new operator, with no arguments, to create a Date object based on the current time and date

    New Date ();
Console.log (new Date ());//tue Jul 13:41:45 gmt+0800 (China Standard Time) Console.log (new date);//tue Jul 13:41:45 gmt+0 800 (Chinese Standard Time) Console.log (typeof new Date ());//' object '

The "2" Date () function can accept a numeric parameter that represents the number of milliseconds between the set time and the 0 O'Clock January 1, 1970

New Date (milliseconds);
Console.log (new Date (0));//thu Jan 1970 08:00:00 gmt+0800 (China Standard Time) Console.log (new Date (86400000));//fri Jan 02 1970 08: 00:00 gmt+0800 (China Standard Time) Console.log (typeof new Date (0));//object

The "3" Date () function accepts a string parameter in the form of a parameter similar to the Date.parse () method. But the parse () method returns a number, and the date () function returns an object

New Date (datestring);
Console.log (New Date (' 6/13/2004 ')),//sun June 2004 00:00:00 gmt+0800 (China Standard Time) Console.log (Date.parse (' 6/13/2004 ')); /1087056000000console.log (typeof new Date (6/13/2004));//objectconsole.log (typeof Date.parse (6/13/2004));//number

The processing of the predecessor 0 in the standard datetime string is also similar to the Date.parse () method, which, if there is a predecessor 0, is equivalent to UTC time, if not, it is equivalent to local time. The rest of the situation is usually local time

Console.log (new date (' 7/12/2016 ')),//tue Jul 00:00:00 gmt+0800 (Chinese Standard Time) Console.log (new date (' 2016-7-12 '));// Tue Jul 00:00:00 gmt+0800 (China Standard Time) Console.log (New Date (' 2016-07-12 '));//tue Jul 08:00:00 gmt+0800 (China Standard Time)

The "4" date () function accepts parameters in the form of a parameter similar to the DATE.UTC () method, but the DATE.UTC () method returns a number of milliseconds and is a UTC time, whereas the date () function returns an object and is the local time

Console.log (new Date (2016,7,12)),//fri 00:00:00 gmt+0800 (Chinese Standard Time) Console.log (+new date (2016,7,12));// 1470931200000console.log (typeof new Date (2016,7,12));//' object ' Console.log (DATE.UTC (2016,7,12));// 1470960000000console.log (typeof Date.utc (2016,7,12));//' number '

[note] When using a method with a parameter similar to the Date.parse () function, if the date object goes out of scope, the browser automatically calculates the date into a range of values, and if the date object goes out of scope using a method similar to the DATE.UTC () function, the browser prompts invalid Date

Console.log (new Date (2016,7,32)),//thu Sep 00:00:00 gmt+0800 (China Standard Time) Console.log (new Date (2016,8,1));//thu Sep 00:00:00 gmt+0800 (Chinese Standard Time) Console.log (new date (' 2016-8-32 '));//invalid Dateconsole.log (new date (' 2016-9-1 ')) ;//thu Sep 00:00:00 gmt+0800 (China Standard Time)

Instance method

The Date object has no properties that can be read and written directly, and all access to dates and times needs to pass the method. Most methods of a Date object fall into two forms: one is to use local time, and one is to use UTC time, which is listed below. For example, Get[utc]day () represents both Getday () and getUTCDay ()

There are 46 instance methods in a Date object, which can be divided into the following 3 classes: To class, get class, set class

"To Class"

The To class method returns a string from the Date object that represents the specified time

ToString ()

Returns the date string for the local time zone

toUTCString ()

Returns the date string for UTC time

Toisostring ()

A string that returns the standard date-time string format of a Date object

toDateString ()

A string that returns the date part of a Date object

toTimeString ()

A string that returns the time portion of a Date object

ToJSON ()

Returns a JSON-formatted date string that is exactly the same as the return result of the Toisostring method

Console.log (new date (' 2016-7-12 '). toString ());//tue Jul 00:00:00 gmt+0800 (Chinese Standard Time) console.log (' 2016-7-12 '). toutcstring ());//mon, 16:00:00 GMTconsole.log (New Date (' 2016-7-12 '). toisostring ());// 2016-07-11t16:00:00.000zconsole.log (new date (' 2016-7-12 '). todatestring ());//tue Jul 2016console.log (' 2016-7-12 '). toTimeString ()//00:00:00 gmt+0800 (China Standard Time) Console.log (New Date (' 2016-7-12 '). ToJSON ());// 2016-07-11t16:00:00.000z

toLocaleString ()

Localization transformation of the toString () method

toLocaleTimeString ()

Localization transformation of the totimestring () method

toLocaleDateString ()

Localization transformation of the todatestring () method

Console.log (new date (' 2016-7-12 '). toString ());//tue Jul 00:00:00 gmt+0800 (Chinese Standard Time) console.log (' 2016-7-12 '). toLocaleString ());//2016/7/12 am 12:00:00console.log (New Date (' 2016-7-12 '). todatestring ());//tue Jul 12 2016console.log (new date (' 2016-7-12 '). tolocaledatestring ());//2016/7/12console.log (new Date (' 2016-7-12 '). toTimeString ());//00:00:00 gmt+0800 (China Standard Time) Console.log (New Date (' 2016-7-12 '). tolocaletimestring ());//12:00:00

"Get Class"

The Date object provides a series of get class methods to get the value of an aspect of an instance object

Before introducing the Get class method, first introduce the valueof () method

ValueOf ()

Returns the number of milliseconds from 0 o'clock January 1, 1970

Therefore, it is convenient to use comparison operators to compare date values

var date1 = new Date (2007,0,1), var date2 = new Date (2007,1,1), Console.log (Date1 > Date2),//falseconsole.log (Date1 < DATE2);//true

GetTime ()

Returns the number of milliseconds from 0 o'clock January 1, 1970, with ValueOf ()

Before ECMASCRIPT5, you can use the GetTime () method to implement the Date.now ()

    Date.now = function () {        return (new Date ()). GetTime ()    }

getTimezoneOffset ()

Returns the time zone difference between the current and UTC, expressed in minutes (8*60=480 minutes), and the returned result takes into account daylight saving time factors

Console.log (new date (' 2016-7-12 '). ValueOf ());//1468252800000console.log (new Date (' 2016-7-12 '). GetTime ());// 1468252800000console.log (New Date (' 2016-7-12 '). getTimezoneOffset ());//-480

GetYear ()

Returns the number of years from 1900 (obsolete)

Get[utc]fullyear ()

Return year (4 digits)

Get[utc]month ()

Return month (0-11)

Get[utc]date ()

Returns the day of the Week (1-31)

Get[utc]day ()

Return day of the week (0-6)

Get[utc]hours ()

Return hour value (0-23)

Get[utc]minutes ()

Returns the minute value (0-59)

Get[utc]seconds ()

Returns the seconds value (0-59)

Get[utc]milliseconds ()

Returns the millisecond value (0-999)

[note] The standard datetime format string, with the parameter set in the form of the predecessor 0, is set to UTC time

Console.log (new date (' 2016-07-12t10:00 '). GetYear ());//116console.log (new Date (' 2016-07-12t10:00 '). getFullYear ()) ;//2016console.log (new Date (' 2016-07-12t10:00 '). getUTCFullYear ());//2016console.log (new Date (' 2016-07-12t10:00 ') . GetMonth ());//6console.log (new Date (' 2016-07-12t10:00 '). getUTCMonth ());//6console.log (' 2016-07-12t10:0 0 '). GetDate ());//12console.log (new Date (' 2016-07-12t10:00 '). getUTCDate ());//12console.log (new Date (' 2016-07-12t10:00 '). GetDay ());//2console.log (new Date (' 2016-07-12t10:00 '). GetUTCDay ());//2console.log (new Date (' 2016-07-12t10:00 '). GetHours ());//18console.log (New Date (' 2016-07-12t10:00 '). getUTCHours ());//10console.log (new Date (' 2016-07-12t10:00 '). getminutes ());//0console.log (new Date (' 2016-07-12t10:00 '). getUTCMinutes ());// 0console.log (new date (' 2016-07-12t10:00 '). getseconds ());//0console.log (new Date (' 2016-07-12t10:00 '). getUTCSeconds ());//0console.log (new Date (' 2016-07-12t10:00 '). getmilliseconds ());//0console.log (new Date (' 2016-07-12t10:00 '). GetutcmilliseconDS ());//0 
The current time is 16:35console.log (new Date (). GetHours ()),//16console.log (new Date (). getUTCHours ());//8

"Set Class"

The Date object provides a series of set class methods used to set various aspects of an instance object

The set method is basically corresponding to the Get method, which passes in a parameter similar to DATE.UTC (), returning the internal number of milliseconds after the adjusted date

[note] The week can only be obtained and cannot be set

SetTime ()

Sets the value of a Date object using the format of milliseconds

var d = new Date (' 2016-07-12t10:00 '); Console.log (D.settime (86400000), d);//86400000 Fri Jan, 1970 08:00:00 gmt+0800 (Chinese standard Quasi-time)

Setyear ()

Set year (obsolete)

var d = new Date (' 2016-07-12t10:00 '); Console.log (D.setyear (+), D,d.getyear ())//963396000000 Wed Jul 12 2000 18:00:00 GMT+0800 (China standard Time) 100

Set[utc]fullyear ()

Set year (4 digits), and optional month and date values

Set[utc]month ()

Set the month (0-11) and the optional date value

Set[utc]date ()

Set the day of the Week (1-31)

var d = new Date (' 2016-07-12t10:00 '); Console.log (D.setfullyear (2015,1,1), d.getfullyear ());//1422784800000 2015console.log (D.setmonth (2), D.getmonth ()),//1425204000000 2console.log (d.setdate), d.getdate ());// 1426845600000 20console.log (d.tolocalestring ());//2015/3/20 6:00:00

Set[utc]hours ()

Set the hour value (0-23) and the optional minute, second, and millisecond values

Set[utc]minutes ()

Set the minute value (0-59), and the optional second and millisecond values

Set[utc]seconds ()

Set the Seconds value (0-59), and the optional millisecond value

Set[utc]milliseconds ()

Set the millisecond value (0-999)

var d = new Date (' 2016-07-12t10:20:30 '); Console.log (D.sethours, d.gethours ());//1468256523000 1console.log ( D.setminutes (2,3), d.getminutes ())//1468256523000 2console.log (D.setseconds (3), d.getseconds ());//1468256523000 3console.log (d.tolocaletimestring ())//1:02:03
var d = new Date (' 2016-07-12t10:20:30 '); Console.log (D.setutchours, d.gethours ());//1468285323000  9console.log (d.setutcminutes (2,3), d.getminutes ()),//1468285323000  2console.log (D.setutcseconds (3), D.getseconds ());//1468285323000  3console.log (d.tolocaletimestring ())//9:02:03

Resources

"1" Es5/date object https://www.w3.org/html/ig/zh/wiki/ES5/builtins#Date_.E5.AF.B9.E8.B1.A1
"2" Ruan one peak JavaScript standard reference Tutorial--standard Library--date object http://javascript.ruanyifeng.com/stdlib/date.html
"3" W3school-javascript Advanced Tutorial--date Object http://www.w3school.com.cn/jsref/jsref_obj_date.asp
"4" JavaScript authoritative Guide (6th edition), part III JavaScript core reference
"5" JavaScript Advanced Programming (3rd Edition), chapter 5th reference type

JavaScript type system-Date Object

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.