JavaScript Date()
Syntax & Examples
Syntax of Date
There are 11 variations for the syntax of Date() constructor. They are:
new Date()This constructor returns a new Date object.
Returns value of type Date.
new Date(value)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
value | optional | An integer value representing the number of milliseconds since January 1, 1970, 00:00:00 UTC. |
This constructor returns a new Date object created from given value.
Returns value of type Date.
new Date(dateString)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
dateString | optional | A string value representing a date. The string should be in a format recognized by the Date.parse() method. |
This constructor returns a new Date object created from given date string.
Returns value of type Date.
new Date(dateObject)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
dateObject | optional | An existing Date object to be copied. |
This constructor returns a new Date object created from given Date object.
Returns value of type Date.
new Date(year, monthIndex)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
This constructor returns a new Date object created from given year and month.
Returns value of type Date.
new Date(year, monthIndex, day)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month. |
This constructor returns a new Date object created from given year, month, and day.
Returns value of type Date.
new Date(year, monthIndex, day, hours)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month. |
hours | optional | An integer value representing the hours. |
This constructor returns a new Date object created from given year, month, day, and hours.
Returns value of type Date.
new Date(year, monthIndex, day, hours, minutes)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month. |
hours | optional | An integer value representing the hours. |
minutes | optional | An integer value representing the minutes. |
This constructor returns a new Date object created from given year, month, day, hours, and minutes.
Returns value of type Date.
new Date(year, monthIndex, day, hours, minutes, seconds)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month. |
hours | optional | An integer value representing the hours. |
minutes | optional | An integer value representing the minutes. |
seconds | optional | An integer value representing the seconds. |
This constructor returns a new Date object created from given year, month, day, hours, minutes, and seconds.
Returns value of type Date.
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)Parameters
| Parameter | Optional/Required | Description |
|---|---|---|
year | required | An integer value representing the year. Must be at least 1000 or 4-digit year. |
monthIndex | required | An integer value representing the month (0 for January, 1 for February, and so on). |
day | optional | An integer value representing the day of the month. |
hours | optional | An integer value representing the hours. |
minutes | optional | An integer value representing the minutes. |
seconds | optional | An integer value representing the seconds. |
milliseconds | optional | An integer value representing the milliseconds. |
This constructor returns a new Date object created from given year, month, day, hours, minutes, seconds, and milliseconds.
Returns value of type Date.
Date()This constructor returns a string representation of the current date and time.
Returns value of type String.
✐ Examples
1 Creating a Date object with the current date and time
In JavaScript, we can use the new Date() constructor to create a Date object representing the current date and time.
For example,
- We create a new Date object
currentDateusingnew Date()with no arguments. - The
currentDateobject contains the current date and time. - We log
currentDateto the console usingconsole.log()method.
JavaScript Program
const currentDate = new Date();
console.log(currentDate);Output
Mon May 31 2024 10:20:30 GMT+0530 (India Standard Time)
2 Creating a Date object from a timestamp
In JavaScript, we can use the new Date() constructor with a timestamp to create a Date object representing a specific moment in time.
For example,
- We create a new Date object
specificDateusingnew Date(1609459200000), which is the timestamp for January 1, 2021. - The
specificDateobject contains the date and time corresponding to the timestamp. - We log
specificDateto the console usingconsole.log()method.
JavaScript Program
const specificDate = new Date(1609459200000);
console.log(specificDate);Output
Fri Jan 01 2021 00:00:00 GMT+0530 (India Standard Time)
3 Creating a Date object with specific date and time components
In JavaScript, we can use the new Date() constructor with specific date and time components to create a Date object.
For example,
- We create a new Date object
specificDateTimeusingnew Date(2021, 0, 1, 10, 0, 0, 0), which represents January 1, 2021 at 10:00:00 AM. - The
specificDateTimeobject contains the specified date and time. - We log
specificDateTimeto the console usingconsole.log()method.
JavaScript Program
const specificDateTime = new Date(2021, 0, 1, 10, 0, 0, 0);
console.log(specificDateTime);Output
Fri Jan 01 2021 10:00:00 GMT+0530 (India Standard Time)
Summary
In this JavaScript tutorial, we learned about Date constructor of Date: the syntax and few working examples with output and detailed explanation for each example.