Format Date in Time Zone (YYYY-MM-DD)
Arguments
date: Date | number —Date object or timestamp (ms). timeZone: string —IANA time zone (e.g., 'UTC', 'America/Los_Angeles').
Returns
string — Solution idea
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.
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.
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
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.
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.
Intl.DateTimeFormat with timeZone option. formatToParts for stable output.
Common mistakes on this challenge
Timezone formatter is not honoring date/timeZone contract
This helper should normalize input and build YYYY-MM-DD from Intl parts. Normalize input with date instanceof Date ? date : new Date(date).Throw on invalid dates using Number.isNaN(d.getTime()).Use Intl.DateTimeFormat(...).formatToPartsand reassembleyear-month-day.
Resources
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.