Skip to main content
The 2024 Developer Survey results are live! See the results
deleted 685 characters in body
Source Link
Nathan Dullea
  • 353
  • 2
  • 6
  • 14

Using countries and timezones npm package:

import {getTimezone} from 'countries-and-timezones';
const australianTimezone = 'Australia/Melbourne';
console.log(getTimezone(australianTimezone));

Prints to the console:

{
  name: 'Australia/Melbourne',
  country: 'AU',
  utcOffset: 600,
  utcOffsetStr: '+10:00',
  dstOffset: 660,
  dstOffsetStr: '+11:00',
  aliasOf: null
}

From there, you can use the utcOffset or dstOffset depending on if it is daylight savings time.

Using countries and timezones npm package:

import {getTimezone} from 'countries-and-timezones';
const australianTimezone = 'Australia/Melbourne';
console.log(getTimezone(australianTimezone));

Using countries and timezones npm package:

import {getTimezone} from 'countries-and-timezones';
const australianTimezone = 'Australia/Melbourne';
console.log(getTimezone(australianTimezone));

Prints to the console:

{
  name: 'Australia/Melbourne',
  country: 'AU',
  utcOffset: 600,
  utcOffsetStr: '+10:00',
  dstOffset: 660,
  dstOffsetStr: '+11:00',
  aliasOf: null
}

From there, you can use the utcOffset or dstOffset depending on if it is daylight savings time.

Post Undeleted by Nathan Dullea
deleted 685 characters in body
Source Link
Nathan Dullea
  • 353
  • 2
  • 6
  • 14

Using date-fns and date-fns-tz (wrappers for the Javascript date object)countries and timezones npm package:

import { differenceInMinutes } from 'date-fns';
import { utcToZonedTime, zonedTimeToUtc getTimezone} from 'date-fns'countries-tz';

const centralAmericanTimeZone = 'America/Chicago';
const centralEuropeanTimeZone = 'Europe/Paris';

// I am in the central zone
const centralAmericanDateTime = new Date(); 
// Converting my time to UTC
const utcDateTime = zonedTimeToUtc(centralAmericanDateTime, centralAmericanTimeZone);
// Converting UTC to Central European
const centralEuropeanDateTime = utcToZonedTime(utcDateTime, centralEuropeanTimeZone);
// Getting the difference in minutes between UTC and Central European time-timezones';
const timeDiffaustralianTimezone = differenceInMinutes(centralEuropeanDateTime, utcDateTime);
/'Australia/ Prints: Central Europe is 420 minutes ahead of UTCMelbourne';
console.log('Central Europe is ', timeDiff, ' minutes ahead of UTC.'getTimezone(australianTimezone));

Using date-fns and date-fns-tz (wrappers for the Javascript date object):

import { differenceInMinutes } from 'date-fns';
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';

const centralAmericanTimeZone = 'America/Chicago';
const centralEuropeanTimeZone = 'Europe/Paris';

// I am in the central zone
const centralAmericanDateTime = new Date(); 
// Converting my time to UTC
const utcDateTime = zonedTimeToUtc(centralAmericanDateTime, centralAmericanTimeZone);
// Converting UTC to Central European
const centralEuropeanDateTime = utcToZonedTime(utcDateTime, centralEuropeanTimeZone);
// Getting the difference in minutes between UTC and Central European time
const timeDiff = differenceInMinutes(centralEuropeanDateTime, utcDateTime);
// Prints: Central Europe is 420 minutes ahead of UTC
console.log('Central Europe is ', timeDiff, ' minutes ahead of UTC.');

Using countries and timezones npm package:

import {getTimezone} from 'countries-and-timezones';
const australianTimezone = 'Australia/Melbourne';
console.log(getTimezone(australianTimezone));
Post Deleted by Nathan Dullea
Source Link
Nathan Dullea
  • 353
  • 2
  • 6
  • 14

Using date-fns and date-fns-tz (wrappers for the Javascript date object):

import { differenceInMinutes } from 'date-fns';
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';

const centralAmericanTimeZone = 'America/Chicago';
const centralEuropeanTimeZone = 'Europe/Paris';

// I am in the central zone
const centralAmericanDateTime = new Date(); 
// Converting my time to UTC
const utcDateTime = zonedTimeToUtc(centralAmericanDateTime, centralAmericanTimeZone);
// Converting UTC to Central European
const centralEuropeanDateTime = utcToZonedTime(utcDateTime, centralEuropeanTimeZone);
// Getting the difference in minutes between UTC and Central European time
const timeDiff = differenceInMinutes(centralEuropeanDateTime, utcDateTime);
// Prints: Central Europe is 420 minutes ahead of UTC
console.log('Central Europe is ', timeDiff, ' minutes ahead of UTC.');