Date
The Date constructor can be used with several types of input:
You can pass values for year, month, date of the month, hour, minute, second, and millisecond, like so:
> new Date(2015, 0, 1, 13, 30, 35, 505); Thu Jan 01 2015 13:30:35 GMT-0800 (PST)You can skip any of the input parameters, in which case they are assumed to be 0. Note that month values are from 0 (January) to 11 (December), hours are from 0 to 23, minutes and seconds 0 to 59, and milliseconds 0 to 999.
You can pass a timestamp:
> new Date(1420147835505); Thu Jan 01 2015 13:30:35 GMT-0800 (PST)If you don't pass anything, the current date/time is assumed:
> new Date(); Fri Jan 11 2013 12:20:45 GMT-0800 (PST)If you pass a string, it's parsed in an attempt to extract a possible date value:
> new Date('May 4, 2015'); Mon May 04 2015 00:00:00 GMT-0700 (PDT)
Omitting new gives you a string version of the current date:
> Date() === new Date().toString();
true
Members of the Date constructor
Following are the members of the Date constructor:
|
Property/method |
Description |
|
|
Similar to passing a string to new > Date.parse('May 5, 2015');
1430809200000
> Date.parse('4th');
NaN
|
|
|
Returns a timestamp but in UTC (Coordinated Universal Time), not in local time. > Date.UTC
(2015, 0, 1, 13, 30, 35, 505);
1420119035505
|
The Date.prototype members
Following are the list of Date.prototype members:
|
Property/method |
Description/example |
|
|
Same as > var d = new Date(2015, 0, 1);
> d.toString();
"Thu Jan 01 2015 00:00:00 GMT-0800 (PST)"
> d.toUTCString();
"Thu, 01 Jan 2015 08:00:00 GMT"
|
|
|
Returns only the date portion of > new Date(2015, 0, 1).toDateString();
"Thu Jan 01 2010"
|
|
|
Returns only the time portion of > new Date(2015, 0, 1).toTimeString();
"00:00:00 GMT-0800 (PST)"
|
|
|
Equivalent to > new Date(2015, 0, 1).toString();
"Thu Jan 01 2015 00:00:00 GMT-0800 (PST)"
> new Date(2015, 0, 1).toLocaleString();
"1/1/2015 12:00:00 AM"
|
|
|
Get or set the time (using a timestamp) of a date object. The following example creates a date and moves it one day forward: > var d = new Date(2015, 0, 1);
> d.getTime();
1420099200000
> d.setTime(d.getTime() +
1000 * 60 * 60 * 24);
1420185600000
> d.toLocaleString();
"Fri Jan 02 2015 00:00:00
GMT-0800 (PST)"
|
|
|
Get or set a full year using local or UTC time. There is also > var d = new Date(2015, 0, 1);
> d.getYear();
115
> d.getFullYear();
2015
> d.setFullYear(2020);
1577865600000
> d;
Wed Jan 01 2020 00:00:00 GMT-0800
(PST)
|
|
|
Get or set month, starting from 0 (January): > var d = new Date(2015, 0, 1);
> d.getMonth();
0
> d.setMonth(11);
1448956800000
> d.toLocaleDateString();
"12/1/2015"
|
|
|
Get or set date of the month. > var d = new Date(2015, 0, 1);
> d.toLocaleDateString();
"1/1/2015"
> d.getDate();
1
> d.setDate(31);
1422691200000
> d.toLocaleDateString();
"1/31/2015"
|
|
|
Get/Set hour, minutes, seconds, milliseconds, all starting from > var d = new Date(2015, 0, 1);
> d.getHours() + ':' + d.getMinutes();
"0:0"
> d.setMinutes(59);
1420102740000
> d.getHours() + ':' + d.getMinutes();
"0:59"
|
|
|
Returns the difference between local and universal (UTC) time, measured in minutes. For example the difference between PST (Pacific Standard Time) and UTC: > new Date().getTimezoneOffset();
480
> 420 / 60; // hours
8
|
|
|
Returns the day of the week, starting from 0 (Sunday): > var d = new Date(2015, 0, 1);
> d.toDateString();
"Thu Jan 01 2015"
> d.getDay();
4
> var d = new Date(2015, 0, 4);
> d.toDateString();
"Sat Jan 04 2015"
> d.getDay();
0
|
ECMAScript 5 additions to Date
Following are the additions to the Date constructor:
|
Property/method |
Description |
|
|
A convenient way to get the current timestamp: > Date.now() === new Date().getTime();
true
|
|
|
Yet another > var d = new Date(2015, 0, 1);
> d.toString();
"Thu Jan 01 2015 00:00:00 GMT-0800
(PST)"
> d.toUTCString();
"Thu, 01 Jan 2015 08:00:00 GMT"
> d.toISOString();
"2015-01-01T00:00:00.000Z"
|
|
|
Used by > var d = new Date();
> d.toJSON() === d.toISOString();
true
|