The Wayback Machine - http://web.archive.org/web/20250101010902/https://www.geeksforgeeks.org/javascript-date-constructor/
Open In App

JavaScript Date() Constructor

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript Date constructor is used to create a new Date object. The value returned will be different on the basis of whether the object is called with or without the new keyword.

If we call the object new keyword a Date object is created otherwise a string representing the current dat-time is returned. Also, if we use this constructor as a function it will return a string containing the present date-time.

Syntax:

new Date(val)
new Date(DateStr)
new Date(DateObj)
new Date(year, monthIndex, day, hours, minutes, seconds, ms)

Parameters:

The date object can be created with or without any parameter

  • val: This is the value of date in Integer format representing the time in milliseconds since Jan,1,1970
  • DateStr: This is the date in String format valid in Date.parse() method.
  • DateObj: This is the date object itself passed as a parameter

Other parameters such as year, mothIndex, day, hours, minutes, second, and ms are optional and are integer values representing the specified year, month, day, hours, minutes, seconds, or milliseconds respectively.

Note: If we do not pass any parameter then the date at which the object is created is stored in the object.

Example 1: In this example, we will create a date using different parameters.

JavaScript
const date1 = new Date();
const date2 = new Date(500);
const date3 = new Date("12-03-2022");
const date4 = new Date(date1);

console.log(date1);
console.log(date2);
console.log(date3);
console.log(date4);

Output
2024-07-12T10:59:12.177Z
1970-01-01T00:00:00.500Z
2022-12-03T00:00:00.000Z
2024-07-12T10:59:12.177Z

Example 2: In this example, we will pass values other than string, integer or date object.

JavaScript
const date1 = new Date(undefined);
const date2 = new Date(null);

console.log(date1);
console.log(date2);

Output: Since undefined is a primitive data type it is converted to NaN but null is converted to zero.

Invalid Date
Thu Jan 01 1970 05:30:00 GMT+0530 (India Standard Time)

Example 3: In this example, we will pass the array as a parameter.

JavaScript
const date1 = new Date(["2020-06-03", "12:10"]);

console.log(date1);

Output: Array is converted to a string and is taken as a string input then converted to Date. This method will not work in Firefox

Wed Jun 03 2020 12:10:00 GMT+0530 (India Standard Time)

Supported Browsers:

  • Chrome
  • Edge
  • Firefox
  • Opera
  • Safari

JavaScript Date() Constructor- FAQs

What does the Date constructor do in JavaScript?

The Date constructor creates a new Date object representing a single moment in time. It can be invoked with or without arguments to create a date object.

Can the Date constructor be used to compare dates?

Yes, Date objects created by the Date constructor can be compared using comparison operators (e.g., <, >, <=, >=).

How does the Date() constructor handle two-digit years?

The Date() constructor interprets two-digit years as 1900 + the given value. For example, new Date(99, 0) represents January 1, 1999.

What are the different ways to initialize a Date object?

  • No arguments: new Date() creates a Date object for the current date and time.
  • Milliseconds: new Date(milliseconds) creates a Date object for the specified number of milliseconds since January 1, 1970, 00:00:00 UTC.
  • Date string: new Date(dateString) creates a Date object from a date string.
  • Date components: new Date(year, month, day, hour, minute, second, millisecond) creates a Date object from individual date and time components.

How does the Date constructor handle invalid month or day values?

If the month or day values are out of range, the Date constructor adjusts the date accordingly (e.g., month 13 will be interpreted as January of the following year).


Similar Reads

three90RightbarBannerImg