Explain Codes LogoExplain Codes Logo

Convert UTC Epoch to local date

javascript
date-conversion
timezones
moment-js
Nikita BarsukovbyNikita Barsukov·Feb 3, 2025
TLDR

Here's a quick snippet to convert UTC Epoch to a local date. This utilizes the JavaScript Date object and considers the UTC Epoch timestamp in milliseconds:

const epoch = 1609459200; // Your Epoch timestamp const localDate = new Date(epoch * 1000).toLocaleString(); // chrono gun set at UTC Epoch, pulls local date trigger console.log(localDate); // The time has arrived...in your timezone!

This method works by multiplying the Epoch timestamp by 1000 to convert to milliseconds, the currency Date object is familiar with. The toLocaleString() provides the local date.

Date, UTC, and Epoch: The Trilogy

Before jumping into code, let’s get some basics right. The Date object in JavaScript works with dates and times. It understands dates from milliseconds since the Epoch, i.e., January 1, 1970, 00:00:00 UTC. Creating a new Date object with milliseconds since Epoch (new Date(0)) sets time travel to the very start - the Epoch!

UTC to Local: Crossing Time Zones

Switching from UTC to local time and vice versa is a common requirement in programming. JavaScript provides methods like setUTCSeconds and getTimezoneOffset to swim across time rivers without carrying the burden of conversions yourself.

Watch-outs in Timezone Tango

When you're dancing around with time zones, it's easy to trip over time quirks. A couple of mis-steps to avoid:

  • Remember to grease your time machine gears with the right oil, JavaScript’s Date expects milliseconds, but Epoch time provides time in seconds.
  • JavaScript's getTimezoneOffset returns the timezone difference in minutes, not seconds, between the local timezone and UTC.
  • Don’t forget the Daylight Saving Time switcheroo; JavaScript handles DST automatically in its Date object.

Libraries to the Rescue: Moment.js and Friends

Dealing with time and especially timezones can feel like constantly running up the down escalator. Luckily, libraries like Moment.js, Day.js and Luxon are there to lend a hand:

  • Moment.js: Can convert from UTC to local time with a simple moment.utc(epoch).local().
  • Day.js: A featherweight contender with a near identical API to Moment.js.
  • Luxon: Built by the developers of Moment.js, it handles immutable date instances effectively.

Conversion Functions: Epoch and JavaScript Date

A clever way to reduce clutter in your code is to wrap the logic into one or two functions. Here are a couple handy utilities:

function epochToJsDate(ts) { return new Date(ts * 1000); // Throwing Epoch into JavaScript's slide of time! } function jsDateToEpoch(d) { return Math.floor(d.getTime() / 1000); // Escape from the JavaScript time jungle, back to Epoch simplicity! }

When Edge Cases Show Up: Leap Seconds, DST and More

Here are a few quirky guests that might show up to your timezone party:

  • Leap seconds: JavaScript's Date object doesn't stand on its toes for the odd extra second here and there.
  • Consistent formatting: Rely on toLocaleString() or similar methods to present time in a user-friendly way.
  • Libraries to the rescue: Disguise your date mishaps and present them in a proper format with libraries such as Moment.js or Luxon.