Timezone Conversion in JavaScript
Convert dates between timezones
IntermediateDate/Time ProgramsExample 5 of 10
timezone-conversion.js
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);
Output
Timezone offset: +00:00 Local: 2024-01-15T10:30:45.123Z UTC: 2024-01-15T10:30:45.123Z New York: 2024-01-15T05:30:45.123Z Tokyo: 2024-01-15T19:30:45.123Z London: 2024-01-15T10:30:45.123Z NY formatted: 01/15/2024, 05:30:45 Available timezones: ["Africa/Abidjan", "Africa/Accra", "Africa/Addis_Ababa", "Africa/Algiers", "Africa/Asmara"] NY to Tokyo: 2024-01-15T19:00:00.000Z
What's going on
Timezone conversion handles different zones.
Timezone Offset
getTimezoneOffset(): Minutes from UTC
Negative = ahead of UTC
Positive = behind UTC
UTC Methods
toUTCString(): UTC string
getUTC* methods: UTC components
Work with UTC timezone
Intl API
Intl.DateTimeFormat: Format with timezone
toLocaleString with timeZone option
List timezones: supportedValuesOf
Conversion
Convert between timezones
Handle daylight saving
Use Intl for accuracy
Best Practices
Store dates in UTC
Convert for display
Use Intl.DateTimeFormat
Handle DST changes