guess timezone name using javascript

98 Views Asked by At

I'm working on React app, I want to detect timezone like this format "Africa/Bangui", but it return it as 'Etc/GMT-1', I know it's my machine problem, since I tried the other browsers.

I tried:

  • Intl.DateTimeFormat().resolvedOptions().timeZone;
  • dayjs guess
  • moment guess

all return the some format: "Etc/GMT-1" How can fix this, and take i consideration other clients that have the problem (in their system) I want always this format "Africa/Bangui"

2

There are 2 best solutions below

2
Syed Danial On

use moment timezone https://momentjs.com/timezone/docs/

moment.tz.guess(); // America/Chicago
// suppose the client's timezone changes to Europe/Berlin
moment.tz.guess(); // America/Chicago
moment.tz.guess(true); // Europe/Berlin
moment.tz.guess(); // Europe/Berlin
2
Durvash Nimje On

const dateTimeFormat = new Intl.DateTimeFormat('en-US');
const resolvedOptions = dateTimeFormat.resolvedOptions();
const timeZone = resolvedOptions.timeZone;

console.log(timeZone); // 'Asia/Calcutta'
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>