JavaScript Reference
The timeEnd() method stops the timer with the specified name . It also displays in
the Console the name and the time passed since the last call to the time() method
with the sam e name . The time() and timeEnd() pair can be used to establish how
much time a certain code fr agment in your pr ogram takes to execute.
E.6 Da te (Core JavaScript)
The Date object is a JavaScript built-in data type allowing you to work with date and
time. Internally, the object stores time value as a number of milliseconds since mid-
night on Jan uary 1, 1970 in Universal Coordinated Time (which is the same as Green-
wich Mean Time). This time stamp is also kn own as the UNIX epoch. According
to the ECMAScr ipt stand ard req uirement, the Date object should be able to represent
any moment, to milliseconds precision, in a period of 100 million days before or after
the UNIX epoch. That’s more than a quarter m illion years to the past and f uture.
Object Instance Creation
Date() //Constructor
Syntax:
new Date()
new Date(milliseconds )
new Date(datestring )
new Date(year , month [, day [, hrs [, mins [, secs [, millis ]]]]])
//Called as a function:
Date() //Returns a string representation of current date and time
Constructs a Date object pointing to a certain moment in time. If you supply no argu-
ments to the con structor, then the created Date object is set to the current date and time
(more precisely, the local date and time as set by the operating system of the computer
running the script). When you pass a sing le numeric argument (milliseconds ), this
argument is under sto od as the number of milliseconds since midnight on January 1,
1970 (UTC), which is the object’s inter nal representation of date and time.
The third of the above constru ctors allows you to use a string representation (an argu-
ment datestring ) of da te and time, which is specified in loc al time, not UTC.
When two or more (up to seven) numeric arguments are passed to the c onstructor, they
specify respective fields of the date and time, again in local time. Note that all but the
first two fields are optional as indicated by the square brackets in the above syntax.
Square bra ckets should not be used in a constructor, they just signal which argum ents
are op tional. For example, if you specify mins , then you must specify day and hrs
as well.
The next table summarizes ranges of possible values for each of the seven date a nd
time fields.
E.6. Date (Core JavaScript) 383
Argument Values
year The year should be specified in full format. For back-
wards compatibility, if this argument is betwe en 0 and
99 inclusive, 1900 is automatically added to it.
month An integer from 0 (January) to 11 (Decemb er).
day An integer from 1 to 31.
hrs Hours, a n integer from 0 (midnight) to 23 (11 p.m.).
mins Minutes, an integer from 0 to 59.
secs Seconds, an integer from 0 to 59.
millis Milliseconds, an integer from 0 to 999.
Finally, you can use Date() as an ordinary g lobal function (with out the new ope rator)
to get a string representation of the present time and date.
Examples:
new Date(3600000) //1 a.m. on January 1, 1970 (UTC)
new Date(2016, 0, 1, 12) //Noon on January 1, 2014 (local time)
new Date("1/1/2016 12:00") //Same as above
Date() //Returns human readable local date and time
Out-of-Range Values of Time and Date Fields
When a value provided to the Date() constructor is out of its valid range, it rolls
over a nd the adjacent value is adjusted accordingly. For example, the expression
Date(2016, 0, 1, 2 6) is equivalent to calling Date(2016, 0, 2, 2), and Date
(2016, 0, 2, -4) is th e same a s Date(2016, 0 , 1, 20). The same happens
when you use out-of-range values with metho ds that set individu al time and date fields
of the Date object like, for example, setHours(), or setDate().
For example, the next c ode fragment checks wheth er a year is a le ap year by attemp t-
ing to set date to February 2 9:
dt = new Date(year, 1, 29);
if (dt.getMonth() == 1) {
//year is a leap year
}
Methods
Since the internal date and time representation of the Date object is the number of
milliseconds since the UNIX epoch, no pr operties exist that could be read and written
directly. Instead , there are methods for settin g and getting individual date and time
fields. T hey take car e of conversion to milliseconds, taking into account a time dif-
ference between UTC a nd local time where ne eded. Most methods are designed in
384 JavaScript Reference
JavaScript Reference
pairs to work with both, local time a nd UTC, which is evident from their names—if
there is “UTC” in the method’s name, then th at method operates using universal time.
In method descriptions below, notation [UTC] means that a method exists with and
without “ U TC” in its name.
get[UTC]Date()
Syntax:
date .getDate()
date .getUTCDate()
Returns the day of the month specified by date , using local (ge tDate()) o r universal
(getUTCDate()) time. The method returns values between 1 and 31.
get[UTC]Day()
Syntax:
date .getDay()
date .getUTCDay()
Returns the day of the week specified by date , using local (get Day()) or universal
(getUTCDay()) time. Th e method returns values from 0 (Su nday) and 6 (Saturday ).
get[UTC]FullYear()
Syntax:
date .getFullYear()
date .getUTCFullYear()
Returns the year as specified by date , using local (getFullYear()) or universal
(getUTCFullYear()) time. T he method always returns a full ye ar value, without any
abbreviations.
Example:
var dt = new Date(-1000); //1 second before midnight 1/1/70
dt.getFullYear() //Returns 1970 in Paris or Sydney (local time)
dt.getUTCFullYear() //Returns 1969 (UTC)
E.6. Date (Core JavaScript) 385
get[UTC]Hours()
Syntax:
date .getHours()
date .getUTCHours()
Returns the hour of the day as defined by date , using local (getHou rs()) or univer-
sal (getUTCHours()) time. The return value follows a 24-hour system, where 0 is
midnight and 23 is 11 p.m.
Example:
var dt = new Date(0); //Midnight 1/1/70
dt.getHours() //Returns 19 in New York (local time)
dt.getUTCHours() //Returns 0 (UTC)
get[UTC]Milliseconds()
Syntax:
date .getMilliseconds()
date .getUTCMilliseconds()
This method returns the milliseconds in the second as specified b y date , using lo-
cal (getMilliseconds()) or universal (getUTCMilliseconds()) time. The return
value is between 0 and 999.
get[UTC]Minutes()
Syntax:
date .getMinutes()
date .getUTCMinutes()
Returns the m inutes in the hour as specified by date , using local (getMinutes ()) or
universal (getUTCMinutes()) time. Th e return value is between 0 and 59 .
get[UTC]Month()
Syntax:
date .getMonth()
date .getUTCMonth()
This method returns the month as defined by date , using local (getMonth()) or uni-
versal (getUTCMonth()) time. The return value is from 0 (January) to 11 (Dec ember).
386 JavaScript Reference
JavaScript Reference
get[UTC]Seconds()
Syntax:
date .getSeconds()
date .getUTCSeconds()
The method returns the seconds in the minute as specified by date , using local
(getSeconds()) or universal (getUTCSeconds()) time. The return value is betwe en
0 and 59.
getTime()
Syntax:
date .getTime()
Returns the number of milliseconds between mid night o n January 1, 1970 (UTC) and
the date and time stored in date . This method is useful, for example, if you want
to establish how mu ch time has elap sed between two moments in date a nd time, or
simply to compare two Date objects.
Note that the number of milliseconds stored inside a Date object is always measur ed
from the UNIX epoch in universal time and is independent of the location, so there is
no getUTCTime() method.
Examples:
var today = new Date(); //Local time
var theMoment = new Date(2017, 2, 13); //Local time
if (today.getTime() < theMoment.getTime()) {
//theMoment is yet to come.
}
var utc = new Date(3600000); //1 am on 1/1/70 (UTC)
var local = new Date(1970, 0, 1, 1); //1 am on 1/1/70 (local time)
//The next expression is true or false, depending on a location.
//It is true in London and false in Tokyo, for example.
utc.getTime() == local.getTime()
getTimezoneOffset()
Syntax:
date .getTimezoneOffset()
E.6. Date (Core JavaScript) 387
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.91.206