JavaScript Date and Time
Create, format, and modify dates in JavaScript with timestamps, UTC, and common calculations.
TL;DR
- Create dates using
new Datewith several input options. - Read date parts with
getFullYear(), change them withsetFullYear(). - Format dates with
toISOString()for machines andtoLocaleDateString()for humans.
Creating Dates
new Date()Accepts four different argument shapes for creating dates.
const now = new Date();From a string or partsParse a date string, or pass year/month/day/time parts.
const d = new Date("2025-10-11");
const custom = new Date(2025, 9, 11, 15, 30);From millisecondsPass a millisecond timestamp to build a date from a number.
const fromMs = new Date(1697040000000);Reading and Changing Parts
getFullYear() / setFullYear()Reads the four-digit year, or sets it to change the date.
now.getFullYear(); // 2025
date.setFullYear(2026);getMonth() / setMonth()Reads or sets the month as a zero-indexed number (0 = Jan).
now.getMonth(); // 9 means October
date.setMonth(0); // JanuarygetDate() / getDay() / setDate()Day of month or weekday; setDate() changes it in place.
now.getDate(); now.getDay(); // 0 = Sunday
date.setDate(1);Timestamps and UTC
Date.now()Returns the current timestamp in milliseconds since the epoch.
const ts = Date.now();getUTC methodsReads times in universal coordinated time, not local timezone.
now.getUTCFullYear(); now.getUTCHours();Time componentsRead hours, minutes, seconds; setHours() sets all three.
now.getHours();
date.setHours(12, 0, 0);Easy comparisonsTimestamps make it easy to compare or subtract two dates.
date1.getTime() < date2.getTime();Formatting and Math
toISOString() / toLocaleDateString()Formats a date for machines (UTC) or a readable local string.
now.toISOString();
now.toLocaleDateString();Adding daysRead the current date and add to it to shift by any amount.
const today = new Date();
today.setDate(today.getDate() + 5);Subtracting datesSubtract two dates to get the difference in milliseconds.
const diffMs = date1 - date2; // millisecondsTips
- For complex date math like time zones, recurring events, or relative time, use a library like
date-fnsorLuxon. - Store dates as ISO strings or timestamps in APIs and databases, converting to a
Dateobject only for display.
Warnings
- JavaScript months are zero-indexed, so passing
0means January and passing11means December, not month 12. - Date string parsing varies by browser for non-ISO formats, so prefer ISO 8601 or
new Date(year, month, day)instead.
In Practice
Creates a target date, computes the whole-day difference from today, and formats both dates for display.
- new Date(year, month, day) builds the deadline — remember month is zero-indexed, so October is 9.
- Subtracting two Date objects returns the difference in milliseconds, not days.
- Dividing by the number of milliseconds in a day and rounding up gives a whole day count.
- toLocaleDateString() formats each date for display while the raw Date objects handle the math.
const today = new Date();
const deadline = new Date(2026, 9, 31); // October 31, 2026 (month is zero-indexed)
const msPerDay = 1000 * 60 * 60 * 24;
const daysLeft = Math.ceil((deadline - today) / msPerDay);
console.log(`Today: ${today.toLocaleDateString()}`);
console.log(`Deadline: ${deadline.toLocaleDateString()}`);
console.log(`${daysLeft} day(s) remaining`);FAQ
JavaScript's Date constructor takes months as zero-indexed values, so January is 0 and December is 11. Always subtract 1 when passing a human-readable month number, e.g. new Date(2024, 0, 15) for January 15th.
Use Date.now() to get milliseconds since the Unix epoch. Divide by 1000 and floor the result to get seconds: Math.floor(Date.now() / 1000). You can also call new Date().getTime() for the same millisecond result.
Read the current day with getDate(), add your offset, then set it back with setDate(), e.g. date.setDate(date.getDate() + 7). JavaScript automatically rolls over the month and year if the value exceeds the month's length. For anything more complex, like adding months or handling DST, use date-fns or Luxon.
Use toLocaleDateString() with a locale and options object: new Date().toLocaleDateString('en-US', { year: 'numeric', month: '2-digit', day: '2-digit' }). For a sortable ISO format use toISOString(), which always returns UTC in the format YYYY-MM-DDTHH:mm:ss.sssZ.