Date, Time & Internationalization (Intl API)

10 questions found

How do you create a Date object representing the current moment?

Beginner
new Date() with no arguments creates a Date object set to the current date and time, based on the system clock, represented internally as milliseconds since the Unix epoch (Jan 1, 1970 UTC).
const now = new Date();
console.log(now.getTime()); // e.g. 1770512400000
Real-world example Timestamping when a user submits a form, for auditing or sorting purposes.

Common follow-ups: How do you create a Date for a specific known date?

Numbers Math & BigInt

Why is Date.parse() considered unreliable for parsing date strings?

Beginner
Date.parse() (and new Date(string)) behavior for non-ISO formats is implementation-defined, so the same string can parse differently across browsers. Only the ISO 8601 format (e.g. '2026-08-08') is guaranteed to parse consistently.
new Date('2026-08-08');        // reliable, ISO format
new Date('08/08/2026');        // ambiguous across locales/engines
Real-world example A bug where a date string parsed fine in Chrome but produced Invalid Date in Safari.

Common follow-ups: What library is commonly used to avoid these parsing inconsistencies?

Error Handling

How do you format a date for a specific locale without a library?

Intermediate
Intl.DateTimeFormat lets you format dates according to locale conventions and customizable options (weekday, month style, etc.), without needing an external date library for basic formatting.
const formatter = new Intl.DateTimeFormat('en-GB', { dateStyle: 'long' });
console.log(formatter.format(new Date())); // '8 August 2026'
Real-world example Displaying order dates in the format users in a specific country expect (DD/MM/YYYY vs MM/DD/YYYY).

Common follow-ups: How would you format just the time portion, not the date?

Design Patterns in JavaScript

How do you calculate the difference between two dates in days?

Intermediate
Subtracting two Date objects (or calling getTime() on each) yields the difference in milliseconds; divide by the number of milliseconds in a day (1000*60*60*24) to get whole days.
const start = new Date('2026-08-01');
const end = new Date('2026-08-08');
const days = (end - start) / (1000*60*60*24); // 7
Real-world example Calculating how many days remain until a subscription renews.

Common follow-ups: Why can this calculation be off by an hour around daylight saving time changes?

Numbers Math & BigInt

How do you format numbers and currency for a locale using Intl.NumberFormat?

Intermediate
Intl.NumberFormat handles locale-aware digit grouping, decimal separators, and currency symbols automatically, avoiding manual and error-prone string manipulation.
const price = new Intl.NumberFormat('de-DE', {
  style: 'currency', currency: 'EUR'
}).format(1234.5);
console.log(price); // '1.234,50 €'
Real-world example Displaying product prices correctly formatted for a user's detected region.

Common follow-ups: How would you format a large number with abbreviations like '1.2K'?

Types & Coercion

Why is manually working with time zones using the Date object error-prone?

Advanced
The built-in Date object always represents a single instant in time and only exposes it in either UTC or the *local system's* time zone — there's no built-in way to reliably format a date in an arbitrary, different time zone without extra options or a library.
// Formatting in a specific, non-local zone requires Intl:
new Intl.DateTimeFormat('en-US', {
  timeZone: 'America/New_York', timeStyle: 'short'
}).format(new Date());
Real-world example Showing a meeting time correctly in both the organizer's and an attendee's time zones.

Common follow-ups: What does the newer Temporal API aim to fix about Date's time zone handling?

Error Handling

How do you sort an array of strings correctly for a given language using Intl.Collator?

Advanced
Default string comparison (< and >) uses UTF-16 code point order, which doesn't match human-expected alphabetical order for many languages (accents, special characters). Intl.Collator provides locale-aware, linguistically correct sorting.
const collator = new Intl.Collator('sv');
['ö', 'z', 'a'].sort(collator.compare); // locale-correct Swedish order
Real-world example Sorting a list of customer names correctly for users in different countries, respecting local alphabetization rules.

Common follow-ups: How does Intl.Collator handle case-insensitive sorting?

Arrays & Array Methods

What is the Temporal API and what problem does it solve?

Advanced
Temporal is a modern proposal/API for date and time handling designed to replace the flawed, mutable Date object with immutable, explicit types (PlainDate, ZonedDateTime, Duration, etc.) that handle time zones and calendars correctly by design.
// Temporal (where available):
const date = Temporal.PlainDate.from('2026-08-08');
const later = date.add({ days: 7 });
console.log(later.toString()); // '2026-08-15'
Real-world example Building a scheduling app where date math needs to be reliably correct across time zones and calendar edge cases.

Common follow-ups: Why has adoption of Temporal been gradual across browsers?

Design Patterns in JavaScript

How do you get a relative time string like '3 days ago' using built-in APIs?

Advanced
Intl.RelativeTimeFormat generates locale-correct relative phrases from a numeric offset and unit, without manually building strings like 'X days ago'.
const rtf = new Intl.RelativeTimeFormat('en', { numeric: 'auto' });
console.log(rtf.format(-3, 'day')); // '3 days ago'
console.log(rtf.format(1, 'day'));  // 'tomorrow'
Real-world example Showing 'Posted 2 hours ago' style timestamps in a social feed or comment section.

Common follow-ups: How would you compute the numeric offset needed for RelativeTimeFormat from two Date objects?

Numbers Math & BigInt

Why does new Date(2026, 7, 8) represent August 8th, not July 8th?

Advanced
The Date constructor's month argument is zero-indexed (0 = January, 11 = December) for historical reasons, so month 7 means the 8th month, August — a very common source of off-by-one date bugs.
const d = new Date(2026, 7, 8);
console.log(d.getMonth()); // 7
console.log(d.toDateString()); // 'Sat Aug 08 2026'
Real-world example A recurring bug class where developers pass the human month number directly and get the wrong month.

Common follow-ups: Does Intl.DateTimeFormat or Temporal also use zero-indexed months?

Error Handling