JavaScript

Lesson 10

Dates and time zones

Format dates with explicit locale and time zone choices instead of manual date math.

Good Code

release-date.js
function formatReleaseDate(isoDate, locale, timeZone) {
  // Locale and time zone are explicit formatting inputs.
  const formatter = new Intl.DateTimeFormat(locale, {
    dateStyle: "medium",
    timeZone,
  });

  return formatter.format(new Date(isoDate));
}

formatReleaseDate("2026-05-24T00:00:00Z", "en-US", "UTC");

Bad Code

release-date.js
function formatReleaseDate(dateString) {
  // Manual formatting depends on the runtime's local time zone.
  const date = new Date(dateString);

  return (
    date.getMonth() + 1 + "/" +
    date.getDate() + "/" +
    date.getFullYear()
  );
}

Review Notes

What to review

Good Code

The good version makes locale and time zone part of the formatting decision, so the same timestamp can be displayed consistently for the intended audience.

Bad Code

The bad version formats by hand using the runtime's local time zone, which can shift the displayed day for users in different regions.

Takeaways

  • Use Intl date formatting and pass the intended time zone when the displayed day matters.