Skip to main content

JavaScript Date Objects

JavaScript Date objects are used to represent dates and times in JavaScript.

They are created using the Date constructor, which can take a variety of parameters.

Here's an example of creating a Date object:

const now = new Date(); // creates a Date object representing the current date and time
const specificDate = new Date(2023, 2, 30); // creates a Date object representing March 30th, 2023
const specificDateTime = new Date(2023, 2, 30, 12, 30, 0); // creates a Date object representing March 30th, 2023 at 12:30 PM

Once you have a Date object, you can access various properties of the date and time it represents, such as the year, month, day, hour, minute, and second.

For example:

const date = new Date(2023, 2, 30, 12, 30, 0);
console.log(date.getFullYear()); // 2023
console.log(date.getMonth()); // 2 (months are 0-indexed, so 2 represents March)
console.log(date.getDate()); // 30
console.log(date.getHours()); // 12
console.log(date.getMinutes()); // 30
console.log(date.getSeconds()); // 0

You can also perform various operations on Date objects, such as comparing them or adding/subtracting time from them.

As an example:

const date1 = new Date(2023, 2, 30);
const date2 = new Date(2023, 2, 31);

console.log(date1.getTime() < date2.getTime()); // true (date1 is before date2)

const oneDayInMillis = 24 * 60 * 60 * 1000;
const nextDay = new Date(date1.getTime() + oneDayInMillis);
console.log(nextDay); // Date object representing March 31st, 2023

Creating Date Objects

You can also create Date objects using the Date() constructor.

The Date() constructor can be called with or without parameters.

When called without parameters, it creates a Date object representing the current date and time:

const now = new Date(); // creates a Date object representing the current date and time
console.log(now); // e.g. Wed Mar 30 2023 13:00:00 GMT-0700 (Pacific Daylight Time)

When called with parameters, it creates a Date object representing the specified date and time.

The parameters can be provided in several formats, such as:

  • Year, month (0-11), and day:
const date1 = new Date(2023, 2, 30);
console.log(date1); // Fri Mar 30 2023 00:00:00 GMT-0700 (Pacific Daylight Time)
  • Year, month (0-11), day, hour, minute, and second:
const date2 = new Date(2023, 2, 30, 12, 30, 0);
console.log(date2); // Fri Mar 30 2023 12:30:00 GMT-0700 (Pacific Daylight Time)
  • A string representing the date and time:
const date3 = new Date("2023-03-30T12:30:00Z");
console.log(date3); // Fri Mar 30 2023 05:30:00 GMT-0700 (Pacific Daylight Time)
  • A Unix timestamp (number of milliseconds since January 1, 1970):
const date4 = new Date(1648693800000);
console.log(date4); // Fri Mar 30 2023 12:30:00 GMT-0700 (Pacific Daylight Time)
note

Note that months are zero-indexed (0 for January, 1 for February, etc.) and times are created in the local time zone by default.

JavaScript new Date()

The new Date() is a constructor function that creates a new Date object representing the current date and time.

Here are some examples of using new Date() to create Date objects:

// Create a new Date object representing the current date and time
const now = new Date();
console.log(now); // e.g. Wed Mar 30 2023 13:00:00 GMT-0700 (Pacific Daylight Time)

// Create a new Date object representing a specific date and time
const specificDate = new Date(2023, 2, 30, 12, 30, 0);
console.log(specificDate); // Fri Mar 30 2023 12:30:00 GMT-0700 (Pacific Daylight Time)

// Create a new Date object from a string representation of a date and time
const dateString = "2023-03-30T12:30:00Z";
const dateFromString = new Date(dateString);
console.log(dateFromString); // Fri Mar 30 2023 05:30:00 GMT-0700 (Pacific Daylight Time)

When creating a Date object from a string representation of a date and time, the string must be in a format recognized by the Date constructor.

The ISO format (YYYY-MM-DDTHH:mm:ss.sssZ) is a widely used and accepted format for representing dates and times in JavaScript.

new Date(date string)

The new Date(dateString) constructor creates a new Date object from a string representation of a date and time.

The string must be in a format recognized by the Date constructor.

As an example:

// Create a new Date object from a string representation of a date and time
const dateString1 = "2023-03-30T12:30:00Z";
const dateFromString1 = new Date(dateString1);
console.log(dateFromString1); // Fri Mar 30 2023 05:30:00 GMT-0700 (Pacific Daylight Time)

// Create a new Date object from a different string representation of a date and time
const dateString2 = "Fri Mar 30 2023 12:30:00 GMT-0700";
const dateFromString2 = new Date(dateString2);
console.log(dateFromString2); // Fri Mar 30 2023 12:30:00 GMT-0700 (Pacific Daylight Time)

In the first example:

  • The dateString1 variable contains a date and time string in the ISO format (YYYY-MM-DDTHH:mm:ss.sssZ).
  • The new Date(dateString1) constructor creates a new Date object representing the date and time in the local time zone.

In the second example:

  • The dateString2 variable contains a date and time string in a different format (ddd MMM DD YYYY HH:mm:ss GMT-XXXX).
  • The new Date(dateString2) constructor creates a new Date object representing the date and time in the specified time zone (GMT-0700 in this case).
caution

The new Date() constructor may be forgiving when parsing date strings, leading to unexpected results if the string is invalid. To ensure more reliable date parsing, it's advisable to use libraries such as Moment.js or date-fns.