FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Format Date in Time Zone (YYYY-MM-DD)

Format Date in Time Zone (YYYY-MM-DD)

intermediate
By FrontendAtlas Editorial · Updated Feb 3, 2026
Implement formatDateInTimeZone(date, timeZone) to return a YYYY-MM-DD string for a given Date (or timestamp) in a specific IANA time zone. Use Intl.DateTimeFormat and handle invalid dates safely.

Arguments

  • date: Date | number — Date object or timestamp (ms).
  • timeZone: string — IANA time zone (e.g., 'UTC', 'America/Los_Angeles').

Returns

string — Formatted date as YYYY-MM-DD in the specified time zone.
Examples
formatDateInTimeZone(new Date('2020-01-01T00:30:00Z'), 'America/Los_Angeles') // => '2019-12-31'

formatDateInTimeZone(1593646200000, 'UTC') // => '2020-07-01'

Solution

Overview

Use Intl.DateTimeFormat with a fixed locale and the provided timeZone. formatToParts lets you build a stable YYYY-MM-DD string without relying on locale-specific formatting.

1

Approach: Intl.DateTimeFormat + formatToParts

Steps:

1) Normalize input to a Date.

2) Throw if the date is invalid.

3) Use Intl.DateTimeFormat with { year, month, day } and the timeZone.

4) Read year/month/day from formatToParts and build YYYY-MM-DD.

Complexity: O(1) time, O(1) space.

export default function formatDateInTimeZone(date, timeZone) {
  const d = date instanceof Date ? date : new Date(date);
  if (Number.isNaN(d.getTime())) throw new TypeError('Invalid date');

  const parts = new Intl.DateTimeFormat('en-US', {
    timeZone,
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  }).formatToParts(d);

  const map = {};
  for (const p of parts) {
    if (p.type !== 'literal') map[p.type] = p.value;
  }

  return `${map.year}-${map.month}-${map.day}`;
}
export default function formatDateInTimeZone(date: Date | number, timeZone: string): string {
  const d = date instanceof Date ? date : new Date(date);
  if (Number.isNaN(d.getTime())) throw new TypeError('Invalid date');

  const parts = new Intl.DateTimeFormat('en-US', {
    timeZone,
    year: 'numeric',
    month: '2-digit',
    day: '2-digit'
  }).formatToParts(d);

  const map: Record<string, string> = {};
  for (const p of parts) {
    if (p.type !== 'literal') map[p.type] = p.value;
  }

  return `${map.year}-${map.month}-${map.day}`;
}

Notes & Pitfalls

Pitfalls
  • Manual offset math fails across daylight saving time changes.
  • Using toISOString always returns UTC, not the target time zone.
  • Locale formatting can vary; formatToParts avoids ambiguity.
Edge cases
  • Invalid dates should throw or be handled explicitly.
  • Dates near midnight can change the local day depending on time zone.
  • Invalid time zones should throw a RangeError from Intl.
Techniques
  • Intl.DateTimeFormat with timeZone option.
  • formatToParts for stable output.

Resources

  • MDN – Intl.DateTimeFormat

Similar questions

Validate Username (Rules + Edge Cases)easySanitize href URL (Block javascript: XSS)intermediateSafe JSON Parse (Fallback on Error)easy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
58 / 88Next →→