• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Justin Joyce

Practical tips and tutorials about software development.

  • Standing Invitation
  • Featured Posts
  • Latest
  • About

Convert between timezones in Javascript

Posted Dec 17, 2022 โ€” Updated Jan 10, 2024

Javascript dates are frequently stored in UTC, or Coordinated Universal Time, specifically so we don’t have to store time-zone-adjusted dates. You’ll often see ISO-formatted date strings ending in Z like this: 2022-12-15T21:07:49.883Z. That Z means “Zulu”, and is just military jargon for “UTC”. Greenwich Mean Time (GMT) is also exactly the same: GMT === UTC === Zulu.

Option 1: Use date.toLocaleString

Lets change that UTC to something more useful, since most internet users are not on UTC:

// Take a date string
const dateString = "2022-12-15T21:07:49.883Z";

// Turn it into a Date object
const dateObject = new Date(dateString);
// Thu Dec 15 2022 16:07:49 GMT-0500 (Eastern Standard Time)
// My browser automatically adjusted the format to EST, my location

dateObject.toLocaleString('en-US', { timeZone: "Asia/Shanghai" });
// '12/16/2022, 5:07:49 AM'

dateObject.toLocaleString('en-US', { timeZone: "Asia/Kolkata" });
// '12/16/2022, 2:37:49 AM'

dateObject.toLocaleString('en-US', { timeZone: "America/Los_Angeles" });
// '12/15/2022, 1:07:49 PM'

// Using locales, you can also get the correct date format
// for the date's location if needed
dateObject.toLocaleString("en-IN", { timeZone: "Asia/Kolkata" })
// '16/12/2022, 2:37:49 am'

dateObject.toLocaleString('zh-cn', { timeZone: "Asia/Shanghai" });
// '2022/12/16 05:07:49'

Option 2: Use Intl.DateTimeFormat

If you’re going to do a lot of converting to a specific time zone or locale, you can also use the Intl.DateTimeFormat built-in:

// this californiaTime object will have a .format() method
// which operates on Date() objects
const californiaTime = new Intl.DateTimeFormat(
    "en-US",
    {
        timeZone: "America/Los_Angeles",
        timeStyle: "long",
        dateStyle: "short",
    }
);

const randomIsoTimes = [
    "2022-01-03T22:21:54Z",
    "2012-12-15T21:31:38Z",
	  "1999-06-01T04:04:28Z",
]

// Now pass each of these dates to our pre-built formatter
randomIsoTimes.map(timeString =>
	californiaTime.format(new Date(timeString))
)
/* 
	[
		  '1/3/22, 2:21:54 PM PST',
	    '12/15/12, 1:31:38 PM PST',
    	'5/31/99, 9:04:28 PM PDT',
	]
*/

Personally, I find the Intl package to be a bit much for most use cases and I usually use .toLocaleString(), but Intl does have a ton of options for customization. Intl is also very useful for reformatting numbers, particularly currencies, but that’ll be for another post.


Helpful Links

  • What is UTC or GMT time? – noaa.gov
  • Wikipedia List of tz database timezones, like Asia/Shanghai

Filed Under: Dates, Javascript

Primary Sidebar

Recent Posts

  • Every Built-In Vim Color Scheme (with screenshots)
  • Reverse a string in Python
  • Meeting Cost Calculator
  • Vim find and replace
  • What makes an effective development team

Categories

  • Arrays (5)
  • Command Line (9)
  • Dates (3)
  • Featured (7)
  • Git (7)
  • Golang (5)
  • Javascript (8)
  • Productivity (8)
  • Projects (4)
  • Python (15)
  • Regex (2)
  • Ruby (3)
  • Shell (2)
  • Thoughts (2)
  • Tips (11)
  • Tools (3)
  • Tutorials (1)
  • Vim (4)

Archives

  • July 2024 (1)
  • February 2024 (1)
  • January 2024 (1)
  • December 2023 (1)
  • November 2023 (1)
  • October 2023 (4)
  • September 2023 (1)
  • August 2023 (2)
  • July 2023 (5)
  • June 2023 (3)
  • May 2023 (6)
  • April 2023 (5)
  • March 2023 (5)
  • February 2023 (10)
  • January 2023 (6)
  • December 2022 (7)

Copyright © 2025 ยท Contact me at justin [at] {this domain}

  • Privacy Policy