How to Create a New Date Object in JavaScript?

Estimated read time 1 min read

In JavaScript, you can create a new Date object by using the Date constructor. The syntax is as follows:

var date = new Date();

This creates a new Date object with the current date and time.

You can also create a Date object with a specific date and time by passing parameters to the constructor. The parameters can be either a date string, a timestamp (number of milliseconds since January 1, 1970, 00:00:00 UTC), or a set of separate date and time values for year, month, day, hour, minute, second, and millisecond.

Here are some examples:

// create a Date object from a date string
var date = new Date("2023-02-09T11:00:00");

// create a Date object from a timestamp
var date = new Date(1612628000000);

// create a Date object from separate values
var date = new Date(2023, 1, 9, 11, 0, 0, 0);

Note that the month parameter is 0-based, so January is 0 and February is 1, etc.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply