Timezone Conversion in JavaScript
Convert time zones in JavaScript with getTimezoneOffset, UTC, toLocaleString and Intl.DateTimeFormat. Store UTC, show local. Explanation and output.
The sample output was captured with the clock set to 15 January 2024, 10:30:45 UTC. Your run will show your own date, time and time zone.
1// Method 1: Get timezone offset2function getTimezoneOffset() {3 const offset = new Date().getTimezoneOffset();4 const hours = Math.floor(Math.abs(offset) / 60);5 const minutes = Math.abs(offset) % 60;6 const sign = offset > 0 ? '-' : '+';7 return `${sign}${String(hours).padStart(2, '0')}:${String(minutes).padStart(2, '0')}`;8}910console.log('Timezone offset:', getTimezoneOffset());1112// Method 2: Convert to UTC13function toUTC(date) {14 return new Date(date.toUTCString());15}1617const localDate = new Date();18console.log('Local:', localDate);19console.log('UTC:', toUTC(localDate));2021// Method 3: Convert to specific timezone (using Intl)22function toTimezone(date, timezone) {23 return new Date(date.toLocaleString('en-US', { timeZone: timezone }));24}2526const date = new Date();27console.log('New York:', toTimezone(date, 'America/New_York'));28console.log('Tokyo:', toTimezone(date, 'Asia/Tokyo'));29console.log('London:', toTimezone(date, 'Europe/London'));3031// Method 4: Format with timezone32function formatWithTimezone(date, timezone) {33 return new Intl.DateTimeFormat('en-US', {34 timeZone: timezone,35 year: 'numeric',36 month: '2-digit',37 day: '2-digit',38 hour: '2-digit',39 minute: '2-digit',40 second: '2-digit',41 hour12: false42 }).format(date);43}4445console.log('NY formatted:', formatWithTimezone(date, 'America/New_York'));4647// Method 5: List available timezones48function getTimezones() {49 return Intl.supportedValuesOf('timeZone');50}5152console.log('Available timezones:', getTimezones().slice(0, 5));5354// Method 6: Convert between timezones55function convertTimezone(date, fromTZ, toTZ) {56 const fromDate = new Date(date.toLocaleString('en-US', { timeZone: fromTZ }));57 const toDate = new Date(date.toLocaleString('en-US', { timeZone: toTZ }));58 return toDate;59}6061const nyDate = new Date('2024-01-15T12:00:00');62const tokyoDate = convertTimezone(nyDate, 'America/New_York', 'Asia/Tokyo');63console.log('NY to Tokyo:', tokyoDate);
Sample Output
UTC: 2024-01-15T10:30:45.000Z
Milliseconds are lost because toUTCString has no milliseconds.
NY to Tokyo: 2024-01-15T21:00:00.000Z
The input was 12:00 in the machine zone (UTC). Tokyo is UTC+9.
In this one
- Why a Date is one moment and zones only change display
- How to read the local offset
- How to format a date in any time zone with Intl
- Why you should store UTC
How it works
- 1
The local offset is printed as +00:00.
- 2
The local date and its UTC version are printed.
- 3
The same moment is shown for New York, Tokyo and London.
- 4
Intl formats New York time as text.
- 5
The first five supported zones are listed.
- 6
convertTimezone reads 12:00 UTC in Tokyo clock time.
Edge cases
Saving the shifted Date
toTimezone(date, "Asia/Tokyo") stored in a database is 9 hours off.
Use those shifted dates for display only. Save the original.
Daylight saving time
New York is UTC-5 in winter and UTC-4 in summer.
Use zone names like America/New_York, not fixed offsets.
fromTZ ignored
convertTimezone gives the same result for any fromTZ.
It reads the moment as given. Use a library for wall-time conversion.
What's going on
A time zone is a region that uses the same clock time. When it is 10:30 in London (in winter), it is 05:30 in New York and 19:30 in Tokyo. An online class for students in many countries must show each person their own time.
A JavaScript Date always holds one exact moment as a UTC timestamp. Time zones only change how it is shown. Keep that idea in mind and this topic gets much easier.
Method 1 reads getTimezoneOffset(). It returns minutes between UTC and local time, with the sign flipped. Negative means ahead of UTC and positive means behind UTC. India returns -330. The helper turns it into a readable +05:30.
Method 2 uses toUTCString, which shows the date in UTC. Building a new Date from that string drops the milliseconds, which is why the UTC line ends in .000Z. getUTC methods such as getUTCHours read UTC parts directly.
Method 3 uses toLocaleString with a timeZone option to get clock text in another zone, then parses it back into a Date. That trick gives a Date whose local fields look like that zone. It is handy for display, but the timestamp is shifted, so do not save it or do math with it.
Method 4 is the clean way: Intl.DateTimeFormat with a timeZone option formats the real moment in any zone. Method 5 lists supported zones with Intl.supportedValuesOf. Names like Asia/Kolkata handle daylight saving rules for you.
Method 6, convertTimezone, has a limit. It ignores fromTZ and treats the input as a real moment, then reads it in Tokyo. True "this wall time in New York" conversion needs a library or the newer Temporal API.
Best practices: store dates in UTC, convert only for display, use Intl.DateTimeFormat, and expect daylight saving changes.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
How should you store dates in a database?
Pick an answer to check it.
Question 2
Which API formats a date in a chosen zone?
Pick an answer to check it.
Questions people ask
Why is the offset sign backwards?
getTimezoneOffset returns UTC minus local time, a rule from early browsers.
Where can I find zone names?
Intl.supportedValuesOf("timeZone") lists them, like Asia/Kolkata.
Why does "NY to Tokyo" show 21:00?
The input is read as 12:00 in the machine zone (UTC here). Tokyo is 9 hours ahead.