JavaScript Date Object: Complete Guide

Learn the JavaScript Date object for working with dates and times. How to create, read, and modify dates with practical examples for beginners.

5 min read

The JavaScript Date object represents a single moment in time. It stores dates as a timestamp (milliseconds since January 1, 1970 UTC) and provides methods to read and modify date components like year, month, day, hours, and minutes.

javascriptjavascript
const now = new Date();
 
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(now.getDate());

If today is July 14, 2026, this prints 2026, 6, and 14. The month is 6, not 7, because JavaScript months are zero-indexed: January is 0, December is 11.

For working with raw timestamps instead of date components, see the guide to working with timestamps.

Creating dates

There are four common ways to create a Date:

javascriptjavascript
const now = new Date();
 
const specific = new Date(2026, 6, 14);
 
const fromString = new Date("2026-07-14");
 
const fromTimestamp = new Date(1721000000000);

The first creates the current moment. The second creates July 14, 2026 (month 6 is July). The third parses an ISO date string. The fourth creates a date from a millisecond timestamp.

When using the year-month-day constructor, the time defaults to midnight local time. You can also specify hours, minutes, seconds, and milliseconds:

javascriptjavascript
const withTime = new Date(2026, 6, 14, 14, 30, 0);

Reading date components

The Date object provides get methods for each component. All return values in the local time zone unless prefixed with UTC.

javascriptjavascript
const now = new Date();
 
console.log(now.getFullYear());
console.log(now.getMonth());
console.log(now.getDate());
console.log(now.getDay());
console.log(now.getHours());
console.log(now.getMinutes());

getFullYear returns the 4-digit year. Use this instead of the deprecated getYear which returns inconsistent values across date ranges.

The other get methods each return one component of the date, all in the local time zone:

MethodReturns
getMonthZero-indexed month, 0 (January) to 11 (December)
getDateDay of the month, 1 to 31
getDayDay of the week, 0 (Sunday) to 6 (Saturday)
getHoursHour, 0 to 23
getMinutesMinute, 0 to 59

Always add 1 to getMonth before displaying it to users, since months are zero-indexed but calendars are not.

For UTC values, use the UTC variants:

javascriptjavascript
console.log(now.getUTCFullYear());
console.log(now.getUTCHours());

Modifying dates

Each get method has a corresponding set method that modifies the date in place. Instead of creating a new date, calling a set method changes the existing Date object and recalculates any related components automatically:

javascriptjavascript
const meeting = new Date(2026, 6, 14);
 
meeting.setDate(meeting.getDate() + 7);
 
console.log(meeting.getDate());

This prints 21 because 7 days were added to the 14th. The Date object automatically handles month and year rollovers: adding 30 days to July 14 correctly produces August 13.

javascriptjavascript
const date = new Date(2026, 6, 14);
 
date.setMonth(date.getMonth() + 2);
 
console.log(date.getMonth());

This prints 8 (September). The original July date becomes September 14. If the target month has fewer days, JavaScript adjusts automatically.

Comparing dates

Dates are compared by converting them to timestamps with getTime:

javascriptjavascript
const deadline = new Date(2026, 11, 31);
const today = new Date();
 
if (today.getTime() < deadline.getTime()) {
  console.log("Still have time.");
}

getTime returns the millisecond timestamp, which is a plain number. Comparing two numbers tells you which moment is earlier. You can also subtract dates directly to get the difference in milliseconds:

javascriptjavascript
const start = new Date(2026, 0, 1);
const end = new Date(2026, 6, 14);
 
const diffMs = end - start;
const diffDays = diffMs / (1000 * 60 * 60 * 24);
 
console.log(diffDays);

This prints the number of days between January 1 and July 14. Divide milliseconds by 1000 for seconds, by 60000 for minutes, by 3600000 for hours, and by 86400000 for days. The result is a plain JavaScript number, so the usual Number type rules for precision and rounding apply to it.

Common mistakes

Forgetting that months are zero-indexed is the most frequent Date bug. new Date(2026, 6, 14) is July 14, not June 14. Always subtract 1 from the human month number when constructing dates, and add 1 when displaying months to users.

Using getYear instead of getFullYear returns a two-digit year for some dates. getYear is deprecated and should never be used in new code.

Parsing date strings without the ISO format can produce inconsistent results across browsers. The string "07/14/2026" may be interpreted differently depending on the user's locale. Stick to "YYYY-MM-DD" ISO strings or the numeric constructor.

Calling Date() without new returns a string, not a Date object. Date() returns the current date as a string. new Date() returns a Date object. The difference is subtle and easy to miss.

Rune AI

Rune AI

Key Insights

  • Create a date with new Date() for now, or new Date(year, month, day) for a specific date.
  • Months are zero-indexed: 0 is January, 11 is December.
  • getFullYear returns the 4-digit year. Avoid getYear, which is deprecated.
  • getDay returns the day of the week (0 = Sunday, 6 = Saturday).
  • Use getTime() to get the timestamp in milliseconds for date arithmetic.
RunePowered by Rune AI

Frequently Asked Questions

Why does getMonth return 0 for January?

JavaScript months are zero-indexed: 0 is January, 1 is February, through 11 is December. This is a legacy design decision from Java's Date class. Always add 1 when displaying months to users.

What time zone does new Date() use?

new Date() creates a date in the user's local time zone. To work in UTC, use methods like getUTCFullYear and getUTCHours instead of getFullYear and getHours.

How do I compare two dates in JavaScript?

Convert both to timestamps with .getTime() and compare the numbers. date1.getTime() < date2.getTime() returns true if date1 is earlier.

Conclusion

The Date object is JavaScript's tool for working with dates and times. Create dates with new Date(), read components with getFullYear and getMonth, and modify them with set methods. The two biggest gotchas are zero-indexed months and the difference between local and UTC methods. For timestamp-based work, see the timestamps guide.