Formatting a Date object as YYYY-MM-DD sounds simple until time zones shift the date forward or backward by a day. Using toISOString().slice(0, 10) only matches what you see on screen if you want UTC—for the user’s local calendar date, format local getters explicitly.


1. Local Calendar Date (YYYY-MM-DD)

Use getFullYear(), getMonth() + 1, and getDate() with padStart(2, "0") so single-digit months and days get a leading zero in the user’s local time zone:

function formatLocalYYYYMMDD(date = new Date()) {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
}

console.log(formatLocalYYYYMMDD(new Date("2026-11-26T23:30:00+05:00"))); // "2026-11-26"

2. When to Use UTC Instead

toISOString() converts the timestamp to UTC (Z) first, which can move an evening local date into the next or previous day:

const d = new Date("2026-11-26T02:00:00+05:00"); // 2026-11-25T21:00:00Z in UTC
console.log(d.toISOString().slice(0, 10)); // "2026-11-25" (UTC date, not local)

If you specifically want the UTC calendar date, use getUTCFullYear(), getUTCMonth() + 1, and getUTCDate() the same way.

3. Common Pitfalls to Avoid

  • Zero-based months: getMonth() returns 0 for January through 11 for December, so always add 1.
  • String date parsing quirks: Parsing a bare "2026-11-26" with new Date("2026-11-26") is treated as UTC midnight by the ECMAScript spec, which can display as "2026-11-25" in negative UTC offsets when read with local getters.
  • Locale formatting differences: toLocaleDateString() changes slash/dash order or separators depending on the user’s locale, so build YYYY-MM-DD directly if strict ISO-like ordering is required.

TL;DR: Use getFullYear(), getMonth() + 1, and getDate() with .padStart(2, "0") for the local YYYY-MM-DD date, and only use toISOString().slice(0, 10) or getUTC*() methods when you intentionally want UTC.

Are you formatting dates for <input type="date">, API payloads, or display labels?