Dates

Updated · 7 min read
mediumES2026javascriptdatetime-zonestimestamps

The Date object represents a single moment in time. Internally it is just a number: the milliseconds since midnight UTC on January 1, 1970 (the Unix epoch). Everything else, the year, month, day and hour you see, is calculated from that number using either UTC or the computer's local time zone. Date has been in JavaScript since 1995 and has well-known traps (months start at 0, it is mutable, parsing is inconsistent), which is why the newer Temporal API exists. You still need Date because it is everywhere in existing code and APIs.

In One Minute

  • new Date() is now; new Date("2026-01-05T10:00:00") is a local time; add Z for UTC.
  • new Date(2026, 0, 5) means January 5: months are numbered 0 to 11.
  • Getters (getFullYear, getMonth, getDate, getDay, getHours...) read local time; getUTC... versions read UTC.
  • date.getTime() and Date.now() give milliseconds since the epoch: compare and subtract those.
  • Date objects are mutable: setters change the object in place.

The examples here were run in the America/Chicago time zone (US Central). If you press Try it elsewhere, local times will differ.

Creating Dates

CodeMeaning
new Date()The current moment
new Date("2026-01-05T10:00:00")January 5, 2026, 10:00 local time
new Date("2026-01-05T10:00:00Z")10:00 UTC (the Z means UTC)
new Date("2026-01-05")Midnight UTC (date-only strings are UTC)
new Date(2026, 0, 5, 10, 30)January 5, 2026, 10:30 local (month 0 = January)
new Date(1767628800000)From a timestamp in milliseconds
create.js▶ Try it
const local = new Date("2026-01-05T10:00:00");
const utc = new Date("2026-01-05T10:00:00Z");
const parts = new Date(2026, 0, 5, 10, 0);
 
console.log(local.toISOString());
console.log(utc.toISOString());
console.log(parts.getTime() === local.getTime());
Output
2026-01-05T16:00:00.000Z
2026-01-05T10:00:00.000Z
true

Explanation: toISOString() always shows UTC. 10:00 in Chicago in January is 16:00 UTC (Central Standard Time is UTC−6), so the local and UTC versions are six hours apart. Building the date from parts with month 0 gives the same moment as the local string.

Months Start at 0

months.js▶ Try it
const d = new Date(2026, 1, 1);
console.log(d.getMonth());
console.log(d.toDateString());
 
console.log(new Date(2026, 12, 1).toDateString());
console.log(new Date(2026, 1, 30).toDateString());
Output
1
Sun Feb 01 2026
Fri Jan 01 2027
Mon Mar 02 2026

Month 1 is February. Out-of-range values roll over instead of failing: month 12 is January of the next year, and February 30 becomes March 2. Days of the month start at 1 as you'd expect; only months (and getDay, where 0 is Sunday) start at 0.

Getters

MethodReturns (local time)UTC version
getFullYear()4-digit yeargetUTCFullYear()
getMonth()0 to 11getUTCMonth()
getDate()Day of the month, 1 to 31getUTCDate()
getDay()Day of the week, 0 (Sun) to 6 (Sat)getUTCDay()
getHours(), getMinutes(), getSeconds(), getMilliseconds()Time partsgetUTCHours()...
getTime()Milliseconds since the epoch (same everywhere)n/a
getTimezoneOffset()Minutes between UTC and local time (UTC minus local)n/a
getters.js▶ Try it
const due = new Date("2026-03-15T17:30:00");
const DAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
 
console.log(due.getFullYear(), due.getMonth() + 1, due.getDate());
console.log(DAYS[due.getDay()], due.getHours(), due.getMinutes());
console.log(due.getUTCHours());
console.log(due.getTimezoneOffset());
Output
2026 3 15
Sun 17 30
22
300

Explanation: March 15, 2026 is after the US switch to daylight saving time (March 8), so Chicago is on Central Daylight Time, UTC−5: 17:30 local is 22:30 UTC, and the offset is 300 minutes. In January, the same code would print an offset of 360.

Setters Change the Date in Place

Date objects are mutable. Setters (setFullYear, setMonth, setDate, setHours...) change the object you call them on, and roll over out-of-range values:

setters.js▶ Try it
const due = new Date("2026-01-28T09:00:00");
due.setDate(due.getDate() + 5);
console.log(due.toDateString());
 
const original = new Date("2026-01-05T09:00:00");
const alias = original;
alias.setMonth(5);
console.log(original.toDateString());
Output
Mon Feb 02 2026
Fri Jun 05 2026

Adding 5 days to January 28 correctly rolled into February. But the second example shows the danger: alias and original are the same object, so changing one changed both. Copy a date before changing it: const copy = new Date(original);.

Timestamps, Comparing and Differences

A date's timestamp (getTime(), or Date.now() for the current moment) is a plain number of milliseconds, which makes comparisons and differences simple:

compare.js▶ Try it
const issued = new Date("2026-01-05T09:00:00");
const paid = new Date("2026-01-19T15:30:00");
 
console.log(paid > issued);
console.log(paid.getTime() === new Date("2026-01-19T15:30:00").getTime());
console.log(paid === new Date("2026-01-19T15:30:00"));
 
const MS_PER_DAY = 24 * 60 * 60 * 1000;
const days = (paid - issued) / MS_PER_DAY;
console.log(days.toFixed(2), Math.floor(days));
Output
true
true
false
14.27 14

Explanation: < and > work on dates because they convert to numbers. === doesn't, because two Date objects are two different objects; compare getTime() values instead. Subtracting two dates gives milliseconds.

Parsing Pitfalls

Only one string format is guaranteed to parse the same way in every engine: the ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ and its shorter forms). Anything else, like "01/05/2026" or "Jan 5, 2026", is parsed in an implementation-specific way:

parsing.js▶ Try it
console.log(new Date("2026-01-05T10:00:00").getDate());
console.log(new Date("not a date").getTime());
console.log(new Date("2026-02-30T00:00:00").toDateString());
Output
5
NaN
Mon Mar 02 2026

An unparseable string gives an Invalid Date, whose timestamp is NaN. Check with Number.isNaN(date.getTime()) before using a parsed date.

The last line is worse: February 30 doesn't exist, but V8 (Chrome, Edge, Node.js) doesn't reject it. It quietly rolls over to March 2. To validate a date someone typed, build it and check that the parts survived: d.getMonth() === month - 1 && d.getDate() === day.

In Practice

The Fee Tracker computes a due date 30 days after an invoice, checks whether a payment was late, and counts calendar days late using noon to avoid daylight saving surprises:

due.js▶ Try it
const MS_PER_DAY = 86_400_000;
 
function addDays(date, days) {
  const copy = new Date(date);
  copy.setDate(copy.getDate() + days);
  return copy;
}
 
function calendarDaysBetween(a, b) {
  const noonA = new Date(a.getFullYear(), a.getMonth(), a.getDate(), 12);
  const noonB = new Date(b.getFullYear(), b.getMonth(), b.getDate(), 12);
  return Math.round((noonB - noonA) / MS_PER_DAY);
}
 
const invoiced = new Date("2026-02-20T09:00:00");
const due = addDays(invoiced, 30);
const paid = new Date("2026-03-27T08:00:00");
 
console.log("Due:", due.toDateString());
console.log("Late by", Math.max(calendarDaysBetween(due, paid), 0), "days");
console.log("Invoice date unchanged:", invoiced.toDateString());
Output
Due: Sun Mar 22 2026
Late by 5 days
Invoice date unchanged: Fri Feb 20 2026

The 30-day period crosses the March 8 daylight saving change, and the calendar-day count is still exact.

Common Mistakes

Forgetting months start at 0. new Date(2026, 1, 5) is February 5. Add 1 when displaying getMonth().

Changing a shared date. Setters mutate. Copy with new Date(date) first.

Comparing dates with ===. Compare getTime().

Parsing non-ISO strings. "01/05/2026" may mean different things in different engines. Use ISO strings or build from parts.

Practice

Challenge: Write nextFirstOfMonth(date) that returns a new Date for the first day of the month after date, at the same time of day, without changing date.

Show solution
first-of-month.js▶ Try it
function nextFirstOfMonth(date) {
  const result = new Date(date);
  result.setDate(1);
  result.setMonth(result.getMonth() + 1);
  return result;
}
 
const d = new Date("2026-01-31T09:00:00");
console.log(nextFirstOfMonth(d).toDateString());
console.log(nextFirstOfMonth(new Date("2026-12-15T09:00:00")).toDateString());
console.log(d.toDateString());
Output
Sun Feb 01 2026
Fri Jan 01 2027
Sat Jan 31 2026

Setting the day to 1 before changing the month matters: January 31 plus one month would otherwise roll over to March 3.

Quick check: Dates

3 questions
1What date is new Date(2026, 2, 1)?
2How do you check whether two Date objects are the same moment?
3What does new Date("not a date").getTime() return?

Key Takeaways

TaskCode
Now (timestamp)Date.now()
Specific local timenew Date("2026-01-05T10:00:00")
Copynew Date(date)
Add daysd.setDate(d.getDate() + n) (on a copy)
Same moment?a.getTime() === b.getTime()
Valid?!Number.isNaN(d.getTime())

Was this tutorial helpful?

Lesson 55 of 139