Craft By Zen

Curation  /  The Stream  /  Intl Locale String with Timezone

Intl Locale String with Timezone

Published

It’s always fun when the MDN documentation doesn’t tell us what the options are within the methods page, I you have to dig deeper into the constructor. The TypeScript bindings don’t always make sense.

MDN - toLocaleString

Anyways, I needed a datetime string with the date, time in hours and minutes, and the timezone. I’ve made the timezone explicit and came up with this snippet.

date.toLocaleString("en-us", {
  year: "numeric",
  month: "short",
  day: "numeric",
  hour: "numeric",
  minute: "numeric",
  timeZone: "America/Los_Angeles",
  timeZoneName: "short",
});

This would return something like “2024-07-03, 12:00 PM, PDT”.