JavaScript Date() constructor



The Date() constructor in JavaScript is used to create a new Date object that represents the current date and time. The constructor can be called with or without the new keyword.

When we call the Date() constructor without any arguments, it returns a string representing the current date and time in the format specified by the system.

When called with arguments, it constructs a Date object representing a specific date and time, based on the provided parameters. If any parameter is out of its valid range, the constructor adjusts the other parameters accordingly. If any parameter is not provided or is NaN, the constructor defaults to the start of the Epoch (January 1, 1970, 00:00:00 UTC).

Syntax

Following are the syntaxes of JavaScript Date() constructor −

new Date()
new Date(value)
new Date(dateString)
new Date(dateObject)

Parameters

Following are the descriptions for above syntaxes −

  • new Date() − When no parameters are passed, it returns the current date and time. The returned timestamp is the same as the value returned by Date.now() method.
  • new Date(value) − An integer value representing a timestamp (the number of milliseconds since the Unix Epoch (January 1, 1970, 00:00:00 UTC).
  • new Date(dateString) − This creates a Date object based on a date string. The dateString parameter can be in different formats such as "YYYY-MM-DD" or "YYYY-MM-DDTHH:MM:SSZ".
  • new Date(dateObject) − This creates a Date object by cloning another Date object. The dateObject parameter represents an existing Date object whose value will be copied.

We can call the Date() with "new" keyword by passing individual date and time component values as parameters as shown below −

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

Parameters

Here, the "new Date()" accepts maximum of 7 parameters −

  • year − An integer value representing the year (e.g., 2024).
  • monthIndex − A numeric index representing the month, starting from 0 for January to 11 for December.
  • day (optional) − The day of the month (1-31).
  • hours (optional) − The hour of the day (0-23).
  • minutes (optional) − The minutes of the hour (0-59).
  • seconds (optional) − The seconds of the minute (0-59).
  • milliseconds (optional) − The milliseconds of the second (0-999).

We can also call the Date() without the "new" keyword. But, calling the Date() (without the "new" keyword) always returns a string representation of the current date and time; regardless of any parameters passed to it.

Date()

Example 1

In the following example, we are calling the Date() constructor with "new" keyword. Also, we are providing any parameters to the constructor −

<html>
<body>
<script>
   const date = new Date();
   document.write(date);
</script>
</body>
</html>

Output

As no parameters is provided to the Date() constructor, it returns the current date and time.

Example 2

This example creates a Date object from the number of milliseconds since January 1, 1970, 00:00:00 UTC (the Unix epoch).

<html>
<body>
<script>
   const dateFromMilliseconds = new Date(1612826400000); //Feb 9, 2021
   document.write(dateFromMilliseconds);
</script>
</body>
</html>

Output

If we execute the above program, it returns a timestamp that represents February 9, 2021.

Example 3

The following example creates a Date object from the provided "date string" −

<html>
<body>
<script>
   const dateFromString = new Date('2024-03-08 18:35:00');
   document.write(dateFromString);
</script>
</body>
</html>

Output

If we execute the above program, it returns the date object based on the provided date string.

Example 4

This example creates a new Date object by copying the value of another Date object −

<html>
<body>
<script>
   const currentDate = new Date();
   const dateFromObject = new Date(currentDate);
   document.write(dateFromObject); // Output: Current date and time
</script>
</body>
</html>

Output

After executing the above program, it returns the current date and time as result.

Example 5

In the example below, we are providing all the parameters to the Date() constructor to create a Date object −

<html>
<body>
<script>
   const specificDate = new Date(2024, 2, 15, 18, 35, 21, 119);
   document.write(specificDate);
</script>
</body>
</html>

Output

If we execute the above program, it returns "Fri Mar 15 2024 18:35:21 GMT+0530 (India Standard Time)" as result.

Example 6

Here, we are calling the Date() constructor without a "new" keyword −

<html>
<body>
<script>
   const date = Date();
   document.write(date);
</script>
</body>
</html>

Output

If we execute the above program, it returns a string representing the current date and time.

Advertisements