Date()
creates a Javascript Date that represents a time.
Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
The signature of Date()
method.
new Date() new Date(value) new Date(dateString) new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]])
To create Date object, use the new
operator.
Parameter | Optional | Description |
---|---|---|
NoParameter | No | the newly-created Date object represents the current date and time as of the time of instantiation. |
value | No | An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC (UNIX epoch). |
dateString | No | A string value representing a date, specified in a format recognized by the Date.parse() method. |
year | No | Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. All other values are the actual year. |
monthIndex | No | Integer value representing the month, beginning with 0 for January to 11 for December. |
day | Optional | Integer value representing the day of the month. The default is 1. |
hours | Optional | Integer value representing the hour of the day. The default is 0 (midnight). |
minutes | Optional | Integer value representing the minute segment of a time. The default is 0 minutes past the hour. |
seconds | Optional | Integer value representing the second segment of a time. The default is 0 seconds past the minute. |
milliseconds | Optional | Integer value representing the millisecond segment of a time. The default is 0 milliseconds past the second. |
To create a new Date object call new Date()
with one of the following arguments:
Code | Description |
---|---|
new Date() | without arguments will create a Date object for the current date and time: |
new Date(milliseconds) | Create a Date object with the time equal to number of milliseconds (1/1000 of a second) passed after the Jan 1st of 1970 UTC+0. // 0 means 01.01.1970 UTC+0 |
new Date(datestring ) | String is parsed automatically as Date.parse |
new Date(year, month, date, hours, minutes, seconds, ms) | Create the date with the given components in the local time zone. Only the first two arguments are obligatory. The year must have 4 digits: 2013 is okay, 13 is not. The month count starts with 0 (Jan), up to 11 (Dec). The date parameter is actually the day of month, if absent then 1 is assumed. If hours/minutes/seconds/ms is absent, they are assumed to be equal 0. For instance: new Date(2011, 0, 1, 0, 0, 0, 0); // 1 Jan 2011, 00:00:00 new Date(2011, 0, 1); // the same, hours etc are 0 by default The minimal precision is 1 ms (1/1000 sec): |
let now = new Date(); console.log( now ); // shows current date/time let jan01_1970 = new Date(0); console.log( jan01_1970 );/*from w w w. j a v a2 s .c om*/ // now add 24 hours, get 02.01.1970 UTC+0 let jan02_1970 = new Date(24 * 3600 * 1000); console.log( jan02_1970 ); let dec31_1969 = new Date(-24 * 3600 * 1000); console.log( dec31_1969 ); let date = new Date("2017-01-26"); console.log(date); let date2 = new Date(2011, 0, 1, 2, 3, 4, 567); console.log( date2 ); let today = new Date() console.log(today); let birthday = new Date('December 17, 1995 03:24:00') console.log(birthday); birthday = new Date('1995-12-17T03:24:00') console.log(birthday); birthday = new Date(1995, 11, 17) console.log(birthday); birthday = new Date(1995, 11, 17, 3, 24, 0) console.log(birthday);
We can set out-of-range values, and it will auto-adjust itself.
let date = new Date(2020, 0, 32); // 32 Jan 2013 console.log(date); //