Skip to content

JavaScript Date() constructor

  • by

JavaScript Date() constructor is used to create a new Date object, which represents a specific moment in time. it is a built-in function and is called in several ways:

new Date()
new Date(value)
new Date(dateString)
new Date(dateObject)
etc....

JavaScript Date() constructor example

Simple example code use date objects and various methods to get and set different parts of the date and time.

<!DOCTYPE html>
<html>
<body>
<script>
    // current date and time
    let currentDate = new Date();
    console.log(currentDate)

    //January 1, 2022
    let newYear = new Date("January 1, 2022");
    console.log(newYear)

    // December 25, 2022 at 12:00 PM
    let christmas = new Date(2022, 11, 25, 12, 0, 0, 0);
    console.log(christmas)

</script>
</body>
</html>

Output:

JavaScript Date() constructor
  1. Without any arguments: If you call new Date(), it will return a Date object that represents the current date and time.
  2. With a string argument: You can pass a string argument to the Date() constructor to create a Date object that represents a specific date and time. The string should be in a format that the Date object can understand, such as “December 17, 1995 03:24:00”.
  3. With multiple numeric arguments: You can pass multiple numeric arguments to the Date() constructor to create a Date object that represents a specific date and time. The arguments should be in the order of year, month, day, hour, minute, second, and millisecond.

Do comment if you have any doubts or suggestions on this JS constructor topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading