skip to Main Content

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

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search