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