DevAcademy
LearnJavaScriptDate and Time
IntermediateJavaScript

Date and Time

Learn how to create and work with JavaScript Date objects — reading and setting date components, working with timestamps, formatting, and performing basic date arithmetic and comparisons.

Reading Time

16 min

Lesson

Lesson 46 of 48

Creating Date Objects

The Date object represents a single point in time, internally stored as the number of milliseconds since January 1, 1970 (the Unix epoch). You can create one in several ways: with no arguments to get the current moment, from a date string, from individual year/month/day/etc. components, or from a timestamp (a raw number of milliseconds).

Four Ways to Create a Date

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Getter Methods

Once you have a Date object, a family of getter methods lets you pull out individual components: getFullYear(), getMonth(), getDate() (the day of the month), getDay() (the day of the week), getHours(), getMinutes(), getSeconds(), and getMilliseconds(). The single most common mistake with these is getMonth() — it's zero-indexed, so January is 0 and December is 11.

Getter Methods in Action

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

The Zero-Indexed Month Gotcha

getMonth() (and the month argument to the Date constructor) treats January as 0 and December as 11. This trips up almost everyone at least once — if you need to display a human-readable month number, remember to add 1, and if you're constructing a date for a specific calendar month, subtract 1 from what you'd normally expect.

Timestamps: getTime() and Date.now()

getTime() returns the number of milliseconds since the epoch for a specific Date object, while Date.now() is a static method that returns the current timestamp directly, without needing to create a Date object first. Timestamps are plain numbers, which makes them convenient for storing, comparing, and doing arithmetic on dates.

Working with Timestamps

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Formatting Dates

For quick, locale-aware formatting, toLocaleDateString() and toLocaleString() are built in and handle a lot of the work for you — they can take a locale string and an options object to control exactly which parts show up and in what style. For anything more involved (custom formats, relative time like "3 days ago", timezone conversions), most real-world projects reach for a dedicated library like date-fns or dayjs rather than hand-rolling the formatting logic.

toLocaleDateString() and toLocaleString()

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Basic Date Arithmetic

Dates can be shifted forward or backward either by working with raw milliseconds and constructing a new Date, or by mutating an existing Date object with its "set" methods (like setDate()), which correctly roll over into the next month or year when needed.

Adding Days Two Ways

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Comparing Dates

Date objects can be compared directly with <, >, <=, and >= — JavaScript converts them to their timestamp value for the comparison. Checking for exact equality is different, though: two separate Date objects representing the same moment are not === equal, since that compares object identity, not the values they hold. Compare their getTime() results instead.

Comparing Two Dates

Try it yourself — edit and run

Console Output

Click “Run” to see the console output here.

Common Date Methods

MethodReturns
getFullYear()The four-digit year
getMonth()The month, 0 (January) through 11 (December)
getDate()The day of the month, 1 through 31
getDay()The day of the week, 0 (Sunday) through 6 (Saturday)
getHours() / getMinutes() / getSeconds()The time-of-day components
getTime() / Date.now()Milliseconds since the epoch

Reach for a Library for Real Formatting Needs

Native Date methods are fine for basic cases, but formatting edge cases, timezone handling, relative time ("2 hours ago"), and date arithmetic across months and years get tricky fast. Libraries like date-fns and dayjs handle these details well and are the standard choice in most production codebases.

Summary

A Date object is built from a string, individual components, a timestamp, or the current moment. Getter methods read out its parts (watch out for the zero-indexed month), getTime() and Date.now() give you raw millisecond timestamps for comparison and arithmetic, and mutating methods like setDate() handle calendar rollovers correctly when you shift a date forward or backward.

Interview Questions

Quick Quiz

1. What does getMonth() return for a date in March?

2. What does Date.now() return?

3. Why might sameMomentA === sameMomentB be false for two Date objects representing the same time?

4. Which method correctly handles rolling over into the next month when adding days?

5. What is commonly recommended for complex date formatting needs?