JavaScript Date()
Syntax & Examples


Syntax of Date

There are 11 variations for the syntax of Date() constructor. They are:

1.
new Date()

This constructor returns a new Date object.

Returns value of type Date.

2.
new Date(value)

Parameters

ParameterOptional/RequiredDescription
valueoptionalAn 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.

3.
new Date(dateString)

Parameters

ParameterOptional/RequiredDescription
dateStringoptionalA 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.

4.
new Date(dateObject)

Parameters

ParameterOptional/RequiredDescription
dateObjectoptionalAn existing Date object to be copied.

This constructor returns a new Date object created from given Date object.

Returns value of type Date.

5.
new Date(year, monthIndex)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn 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.

6.
new Date(year, monthIndex, day)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn 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.

7.
new Date(year, monthIndex, day, hours)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month.
hoursoptionalAn 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.

8.
new Date(year, monthIndex, day, hours, minutes)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month.
hoursoptionalAn integer value representing the hours.
minutesoptionalAn 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.

9.
new Date(year, monthIndex, day, hours, minutes, seconds)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month.
hoursoptionalAn integer value representing the hours.
minutesoptionalAn integer value representing the minutes.
secondsoptionalAn 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.

10.
new Date(year, monthIndex, day, hours, minutes, seconds, milliseconds)

Parameters

ParameterOptional/RequiredDescription
yearrequiredAn integer value representing the year. Must be at least 1000 or 4-digit year.
monthIndexrequiredAn integer value representing the month (0 for January, 1 for February, and so on).
dayoptionalAn integer value representing the day of the month.
hoursoptionalAn integer value representing the hours.
minutesoptionalAn integer value representing the minutes.
secondsoptionalAn integer value representing the seconds.
millisecondsoptionalAn 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.

11.
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,

  1. We create a new Date object currentDate using new Date() with no arguments.
  2. The currentDate object contains the current date and time.
  3. We log currentDate to the console using console.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,

  1. We create a new Date object specificDate using new Date(1609459200000), which is the timestamp for January 1, 2021.
  2. The specificDate object contains the date and time corresponding to the timestamp.
  3. We log specificDate to the console using console.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,

  1. We create a new Date object specificDateTime using new Date(2021, 0, 1, 10, 0, 0, 0), which represents January 1, 2021 at 10:00:00 AM.
  2. The specificDateTime object contains the specified date and time.
  3. We log specificDateTime to the console using console.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.