Explain Codes LogoExplain Codes Logo

Convert UTC date time to local date time

javascript
date-conversion
time-zone
moment-js
Alex KataevbyAlex Kataev·Nov 6, 2024
TLDR

For an express transformation of a UTC timestamp to local time zone in JavaScript, the Date object and the toLocaleString() method come in handy:

const utcDate = new Date('2023-01-28T15:00:00Z'); const localDate = utcDate.toLocaleString(); console.log(localDate); // Converts your UTC time into local time. Magic? No, it's JavaScript!

Note: new Date() digests the UTC string, while toLocaleString() localizes it.

Clean conversion: local, format, DST

Time zone conversions become intrinsic when dealing with applications that span across multiple time zones. Your goal is to bring out accurate conversions, accounting for the user's exact local time zone and Daylight Saving Time (DST).

Seizing the user's timezone

Grab the time zone offset of your user with this introduction:

const currentOffset = new Date().getTimezoneOffset() / 60; console.log(`Current offset: ${currentOffset} hours. Time travel, anyone?`); // Gets user's time zone offset

Mastering Daylight Saving

Daylight saving time (we've all been robbed of an hour's sleep) can be accurately handled with libraries like moment.js or luxon.js. No need for manual adjustments, let them do the heavy lifting!

Catering to the user

Use JavaScript's built-in toLocaleString() method for displaying times, it tailors to the user's locale and timezone settings!

Introducing robust assistants

Done are the days of painful date and time handling, say hello to moment.js and the consistent ISO 8601 format.

Employing moment.js

const moment = require('moment'); // Get Moment.js onboard const localTime = moment.utc(utcDate).local().format('YYYY-MM-DD HH:mm:ss'); console.log(localTime); // "Hold my beer," - Moment.js

ISO 8601 to the rescue

Encourage your server to serve dates in the ISO 8601 format. It cuts out possible inconsistencies and avoids you having to play “Whack-A-Mole" with date anomalies.

Good practices cooker

  1. Consistency: Make sure that the UTC date you use for the conversion is consistent and reliable.
  2. Accuracy: When converting to local time, ensure the result reflects the user's current time zone, and don't forget DST.
  3. Testing: Leave no stone unturned, test across various time zones and different browsers for compatibility.
  4. Documentation: Exploit official documentation like MDN Web Docs, an ocean of resources is waiting.

Hoarding local time

After conversion, you may want to hang onto the local time in a variable. It comes handy if you are to display the local time to the user or reuse the converted value.

Amplify your time traveller vibes:

let userLocalTime = localTime; // Stash this for future adventures

You can now employ this variable where you need it, ensuring the local time displayed to the user always trumps!

Function wave magic

Bundle all the conversion logic into a utility function. Makes your code as organized as the bookshelf you've always dreamed of.

function convertUtcToLocal(utcDateStr){ const date = new Date(utcDateStr); return date.toLocaleString(); } console.log(convertUtcToLocal('2023-01-28T15:00:00Z')); // Converting UTC to local, one date at a time!

To employ this spell, supply the UTC date string you summon from your server.