JavaScript type system-Dates Date object Comprehensive understanding of the basics

Source: Internet
Author: User
Tags constructor current time date1 getdate instance method string format time and date tojson

Front.

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

A static method describes a static method before introducing the constructor of a date object. 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 wrapped in a coat

The Date object has a total of three static methods, namely Date.now (), Date.parse (), DATE.UTC (). These methods are invoked 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 from January 1, 1970 0 o'clock UTC for the current time. The method does not support passing parameters

[Note] This method returns the number numeric type

Console.log (Date.now ());//1468297046050
Console.log (Date.now (' 2016,1,1 '));//1468297046050
Console.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 June 2016 12:21:33 gmt+0800 (China Standard Time)
Console.log (+new date ());//1468297293433
Console.log (+new Date (2000,1,1));//949334400000

This method is often used to analyze the work of code

var start = Date.now ();
DoSomething ();
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 resolve, and returns the number of milliseconds from January 1, 1970 0 o'clock to the given date

The method resolves the format of the string based on the date-time string formatting rules , which are supported in addition to the standard format. If the string is not recognized, it returns a Nan

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

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

3, ' Day of the week: minutes: Second time zone ' Tue may 00:00:00 GMT-0700

[note] The browser does not support string formats that do not represent dates that represent time

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

[note] In ECMAScript5, if you use a standard date-time string format rule string, Math has a front 0 that resolves to UTC and the time does not have a 0 before it resolves to local time. Other situations typically resolve to local time

Console.log (Date.parse (' 7/12/2016 '));//1468252800000
console.log (' 2016-7-12 ') date.parse
Console.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 representing the year, month, day, time, minutes, seconds, and milliseconds, respectively.

DATE.UTC (Year,month,day,hours,minutes,seconds,ms), year and month parameters are fixed, the rest of the parameters are optional, date-time format rules detailed in this

Because the function has 7 formal parameters, its length value is 7

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

[Note] This method uses UTC time, not local time

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

There are as many as 5 ways to use the constructor date () constructor

"0" Date ()

Numbers 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 June 2016 13:38:41 gmt+0800 (China Standard Time)"
Console.log (Date (' 2016/1/1 '));/"Tue June 12 2016 13:38:41 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 June 2016 13:41:45 gmt+0800 (China Standard Time)
Console.log (new date)//tue 12 2016 13:41:45 GMT+0800 (China Standard Time)
Console.log (typeof new Date ());//' object '

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

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

The "3" Date () function accepts a string parameter in a form 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 00:00:00 gmt+0800 (China Standard Time) Console.log (
' Date.parse );//1087056000000
Console.log (typeof new Date (6/13/2004))//object Console.log
(typeof Date.parse (6/ 13/2004));//number

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

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

The "4" Date () function accepts arguments in the form of arguments similar to those of the DATE.UTC () method, but the DATE.UTC () method returns a millisecond, UTC time, and the date () function returns an object and is local time

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

[note] When you use a method that is similar to the Date.parse () function, if the date object is out of range, the browser automatically evaluates the date to a range value, and if the date object is out of range, the browser prompts invalid Date

Console.log (new Date (2016,7,32)),//thu Sep 2016 00:00:00 gmt+0800 (China Standard Time)
Console.log (new Date (2016,8,1)); Thu SEP 2016 00:00:00 gmt+0800 (China Standard Time)
Console.log (new date (' 2016-8-32 '));//invalid Date
Console.log (new Date (' 2016-9-1 '));//thu Sep 2016 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 requires a method. Most methods of a Date object are in two forms: one is to use local time, the other is to use UTC time, and these methods are listed below. For example, Get[utc]day () also represents Getday () and getUTCDay ()

The Date object has a total of 46 instance methods, which can be divided into the following 3 categories: 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 a date string for the local time zone

toUTCString ()

Returns a date string for UTC time

Toisostring ()

Returns the standard date-time string format string for a Date object

toTimeString ()

String that returns the time part of the Date object

Tojson ()

Returns a JSON-formatted date string that exactly matches the return result of the Toisostring method

Console.log (new date (' 2016-7-12 '). toString ());//tue June 2016 00:00:00 gmt+0800 (China Standard Time)
Console.log (new Date (' 2016-7-12 '). toUTCString ())//mon, June 2016 16:00:00 GMT Console.log (
new Date (' 2016-7-12 '). toisostring ()); 2016-07-11t16:00:00.000z
Console.log (New Date (' 2016-7-12 '). toDateString ())//tue June 2016
Console.log ( New Date (' 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 ()

Localized conversion of the ToString () method

toLocaleTimeString ()

Localized conversion of the toTimeString () method

toLocaleDateString ()

Localized conversion of the toDateString () method

Console.log (new date (' 2016-7-12 '). toString ());//tue June 2016 00:00:00 gmt+0800 (China Standard Time)
Console.log (new Date (' 2016-7-12 '). toLocaleString ());//2016/7/12 Morning 12:00:00
console.log (New Date (' 2016-7-12 '). todatestring ());//tue June 2016
Console.log (new date (' 2016-7-12 '). tolocaledatestring ());//2016/7/12
Console.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 for getting 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 January 1, 1970 0 O'Clock

Therefore, you can easily 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);//false
Console.log (Date1 < date2);//true

GetTime ()

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

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

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

getTimezoneOffset ()

Returns the time zone difference between the current and UTC, in minutes (8*60=480 minutes), and returns the results taking into account the daylight saving factors

Console.log (new date (' 2016-7-12 '). valueof ());//1468252800000
Console.log (new date (' 2016-7-12 '). GetTime ());// 1468252800000
Console.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 ()

Back to day (1-31)

Get[utc]day ()

Back to the day of the Week (0-6)

Get[utc]hours ()

Return hour value (0-23)

Get[utc]minutes ()

Return minutes value (0-59)

Get[utc]seconds ()

Return second value (0-59)

Get[utc]milliseconds ()

Returns the millisecond value (0-999)

[note] The UTC time is set through a standard date-time format string with parameter settings in the form of a predecessor 0

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

"Set Class"

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

The Set method basically corresponds to the Get square method, which passes in a parameter similar to DATE.UTC () and returns the internal number of milliseconds for the adjusted date

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

SetTime ()

Sets the value of a Date object using the millisecond format

var d = new Date (' 2016-07-12t10:00 ');
Console.log (D.settime (86400000), d);//86400000 Fri (China Standard Time) 1970 08:00:00

Setyear ()

Set year (obsolete)

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

Set[utc]fullyear ()

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

Set[utc]month ()

Set month (0-11), and optional date values

Set[utc]date ()

Set the day of the first (1-31)

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

Set[utc]hours ()

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

Set[utc]minutes ()

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

Set[utc]seconds ()

Set a second value (0-59) and an optional millisecond value

Set[utc]milliseconds ()

Set millisecond value (0-999)

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

The above JavaScript type system-Date object full understanding is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.