JavaScript/The legacy Date object
[i]Learn· Representing dates & times

The legacy Date object

Note: The Date object is now considered legacy and should be avoided in new code. This section is purely for those working with legacy code.

To create a Date object:

const dateObjectName = new Date();

You can pass parameters of various types:

  • Nothing: creates today's date and time. For example, today = new Date();.
  • A string representing a date, in many different forms. The exact forms supported differ among engines, but the following form is always supported: YYYY-MM-DDTHH:mm:ss.sssZ. For example, xmas95 = new Date("1995-12-25"). If you omit hours, minutes, or seconds, the value will be set to zero.
  • A set of integer values for year, month, and day. For example, xmas95 = new Date(1995, 11, 25).
  • A set of integer values for year, month, day, hour, minute, and seconds. For example, xmas95 = new Date(1995, 11, 25, 9, 30, 0);.

The Date object is similar to a Temporal.ZonedDateTime object, storing a millisecond epoch offset with the ISO 8601 calendar assumed. It represents the same instant in UTC and the local time zone. Methods for handling dates and times fall into these broad categories:

  • "set" methods, for setting date and time values in Date objects.
  • "get" methods, for getting date and time values from Date objects.
  • "to" methods, for returning string values from Date objects.
  • parse and UTC methods, for parsing Date strings.

For example, suppose you define the following date:

const xmas95 = new Date("1995-12-25");

Then xmas95.getMonth() returns 11, and xmas95.getFullYear() returns 1995.

The getTime and setTime methods are useful for comparing dates. The getTime method returns the number of milliseconds since the epoch for a Date object.

For example, the following code displays the number of days left in the current year:

const today = new Date();
const endYear = new Date(today.getFullYear(), 11, 31, 23, 59, 59, 999);
const msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
let daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
daysLeft = Math.round(daysLeft); // Returns days left in the year

This example creates a Date object named today that contains today's date. It then creates a Date object named endYear representing the end of the current year. Then, using the number of milliseconds per day, it computes the number of days between today and endYear, using getTime and rounding to a whole number of days.

The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the ipoDate object:

const ipoDate = new Date();
ipoDate.setTime(Date.parse("Aug 9, 1995"));

There are many undesirable aspects of Date that led to it being abandoned. For example, Date objects are mutable; the string format accepted by the constructor and Date.parse() can't be standardized; it doesn't accept other fixed time zones, etc. The Temporal object is built from the ground up and represents standard industry practices for handling dates. Use Temporal, and install a polyfill if your environment doesn't support it.

Cookbook

You can find snippets for handling common use cases in our Temporal cookbook.

Source: Representing dates & times by Mozilla Contributors, licensed under CC-BY-SA-2.5. Retrieved 2026-11-18. Changes: Extracted the section "The legacy Date object"; added the short section after it ("Cookbook"); removed the code-block language labels and the previous/next links; moved headings up one level; made links absolute; examples are the page's own code blocks, unchanged.