| by Arround The Web | No comments

JavaScript Date() Constructor

The date object is used in JavaScript to manipulate date and time. Working with the date and time in JavaScript is often performed through the JavaScript Date() object. It has several methods and a constructor that simply allows us to work with date and time. On a web page, a timer can be set using the JavaScript date object.

This manual will elaborate on the Date() constructors in JavaScript.

What is JavaScript Date() Constructor?

For creating Date objects, utilize the “new” operator. There are four different Date() constructors available for creating date objects:

    • Date()
    • Date(dateString)
    • Date(milliseconds)
    • Date(year, month, day, hours, minutes, seconds, milliseconds)

Let’s check out each of the mentioned methods one by one!

How to Use Date() Constructor in JavaScript?

By calling the “new Date()” constructor, a new date object is created with today’s date and time:

var date = new Date();

 
Now, print the current date and time on the console by passing the variable “date” to the “console.log()” method:

console.log(date);

 
The following date value will be displayed on the console:

How to Use Date(dateString) Constructor in JavaScript?

In order to construct a new date object with a provided date string, use the “new Date(dateString)” constructor.

To do so, first, we will create a new date object by passing date as a string in the Date(dateString) constructor:

var date = new Date("October 8, 2022 15:11:05");

 
Then, print it on the console:

console.log(date);

 
The corresponding output will look like this:

How to Use Date(milliseconds) Constructor in JavaScript?

With the help of the “new Date(milliseconds)” constructor, a new date object is created by utilizing Universal Time (UTC) by adding the milliseconds.

When the new Date(milliseconds) constructor is invoked, a new date object is created with ZERO milliseconds added to the zero time:

var date = new Date(0);

 
Print the date returned by the “new Date(milliseconds)” constructor using the console.log() method:

console.log(date);

 
Output


Similarly, when we have passed “500000000000” milliseconds to the constructor, the date will be displayed with respect to it:

var date = new Date(500000000000);

 
The given output shows the time after 15 years:

How to Use Date(year, month, day, hours, minutes, seconds, milliseconds) Constructor in JavaScript?

This constructor accepts a minimum of two arguments and a maximum of seven to get the time in the specified format. However, in the case of one parameter, the Date() constructor will accept it as milliseconds.

For instance, we will pass all the parameters to the Date() constructor, including year, month, day, hours, minutes, seconds, and milliseconds as 2022, 5, 11, 15, 14, 15, and 7, respectively:

var date = new Date(2022, 5, 11, 12, 14, 15, 7);

 
Finally, print the date object value on the console using the “console.log()” method:

console.log(date);

 
Output


We have gathered all the essential instructions related to the JavaScript Date() constructor.

Conclusion

To construct a date object, you can utilize one of four variants of the Date() constructor, including Date(), Date(dateString), Date(milliseconds), and Date(year, month, day, hours, minutes, seconds, milliseconds). Moreover, to create a Date object, use the “new” operator. This manual has elaborated on the Date() constructor in JavaScript.

Share Button

Source: linuxhint.com

Leave a Reply