94

I just wrote a little piece of code to show the server time in a webpage. At the moment I just have one machine so I cannot test if the code is working.

Is there a way to tell the browser to use a time configuration different from the one configured in the OS? I have used plugins for Firefox to test different locales, I wonder if there are similar options for time tests.

Thanks.

2

11 Answers 11

160

Create a new empty directory for a separate Chrome user profile. E.g. with

mkdir ~/chrome-profile

You specify the TZ environment variable. You can see the valid timezones for example here, in column TZ.

To start Chrome, use these commands:

  • for Mac OS X: TZ='US/Pacific' open -na "Google Chrome" --args "--user-data-dir=$HOME/chrome-profile"
  • for Linux: TZ='US/Pacific' google-chrome "--user-data-dir=$HOME/chrome-profile"
9
  • 6
    For me, on Mac OSX, had to type the full path instead of ~/chrome-profile shortcut, but other than that worked great! This should be the accepted answer...
    – Peter P.
    Commented Oct 19, 2016 at 0:08
  • 1
    Amazing! Thank you. Also, I renamed the chrome-profile dir to have more specific name for my needs. Commented May 5, 2017 at 7:38
  • 3
    Fantastic answer addressing the core issue and it doesn't require changing the OS timezone. And, yes, this should be the accepted answer as it enables testing with Chrome to address the OP's question. Commented Jul 16, 2017 at 0:08
  • 4
    The chrome profile part wasn't necessary for me. I simply ran TZ='US/Pacific' open -na "Google Chrome" --args on macos. works perfectly
    – connorbode
    Commented Nov 24, 2017 at 21:02
  • 5
    How can I do this on windows? Commented Feb 9, 2018 at 19:15
46

You don't need to change OS variables or extensions to do this in 2021.

Chrome Dev Tools > Hamburger Menu > More Tools > Sensors

On the Location section select your location or give it a timezone id (e.g. America/Chicago, America/New_York) enter image description here

35

Unfortunately, JavaScript is only aware of the current time zone, as it is set by the operating system. There are no facilities to let the Date object use a different time zone in a particular context. There are some libraries to do time zone conversions, but that won't help for what you are asking.

On Linux, Mac OSX, and other *NIX systems, you can set the TZ environment variable. See Benedikt Köppel's answer for details.

However, there is no facility for this on Windows. Some Windows programs may pick up on the TZ environment variable, but those that do will not necessarily interpret it correctly, as they aren't wired up to support IANA time zone names. More on this here and here.

3
  • Thanks for your answer. I'm using PHP. I'm not aware of the full requirements, a friend of mine asked me to help him to display it.
    – Marcelo
    Commented May 8, 2013 at 20:14
  • 13
    Chrome 45 now updates the timezone immediately, but Firefox 40 still doesn't.. and I hope that never changes, or else we're screwed! Stupid timezones! Developer's plague..
    – Alkanshel
    Commented Sep 16, 2015 at 20:06
  • 4
    @Pacerier Not anymore. Chrome will immediately detect the timezone settings of your OS. Example: in the console, enter new Date(). Change your timezone. Enter new Date() in the console again. Each date will show whichever timezones you had set before and after you changed it.
    – keeehlan
    Commented Aug 16, 2016 at 22:37
8

On popular demand (kidding!), the explanation.

Google Chrome reads the environment variables to get the timezone its running in. Apparently all instances of Chrome share the value (not tested). In order to force a timezone, we need to a) set the environment variable to the timezone we want, b) kill all existing instances of Chrome, c) Print the current timezone to confirm, and d) Start Chrome with the intended timezone.

The below shell script is tested with OS X but should work for others too.

export TZ='US/Pacific'

killall Chrome
date
open /Applications/Google\ Chrome.app
3
  • 3
    While this code sample may possibly answer the question, it would be preferable to include some essential explanation to your answer. As it stands now this answer adds little to no value for future readers.
    – oɔɯǝɹ
    Commented Feb 8, 2015 at 21:49
  • 1
    killall is dangerous dude.. I'll edit the answer to add some warning to it.
    – Pacerier
    Commented Apr 9, 2016 at 14:41
  • 6
    @Pacerier so killallin bold will explain why it is dangerous or even warn people that it is dangerous...?
    – henk
    Commented Nov 27, 2017 at 22:34
6

Chrome seems to update its TimeZone as soon as you change it in Windows. Firefox seems to store the timezone value of the system at startup.

With Visual Studio it looks like you can turn the trick on its head. It seems that the Timezone is only read when you first start the debug process. So In my case, what I did was set the TimeZone to, for example, Eastern Standard Time. Start the Visual Studio debugger. I then changed the TimeZone to, for example, Pacific Standard time. The server at this point remained in the Eastern timezone, while my browser was in the Pacific timezone. Now I was able to test the client browser behavior just fine.

If you wanted to test a client in Japan, then just update your TimeZone and the browser will follow it. As long as you don't restart the debug process your server timezone will not change. Also just changing the time (as opposed to the timezone) did not work.

5

There is a lot of time zone extensions available to spoof in browser level. I am using currently enter image description here

After adding click option of the extension and check enter image description here

Once both done, Go to any website and open developer tools and check by executing below command

new Date().toLocaleTimeString()
1
  • Just to add to your answer, if you are using a VPN then you can fetch timezone from the IP. Commented Apr 11, 2021 at 9:33
2

Old question, but I had a similar issue and I'll throw my fix in anyways.

What I did was run my server in a virtual machine, set the network settings to bridged so that I'd be able to access the server from my host OS as the client easily.

I then changed time settings in the VM so that there was whatever difference I wanted between the client and server.

1

This an easies way I tried to chang time zone and test it for windows 7 ^ I use tzutil

tzutil /s "Eastern Standard Time" and save it to suitable .bat file e.g Eastern_Standard_Time_zone.bat and use default_time_zone.bat conain your original system time zone to get it back after finishin tesing . this will change your time zone in blink . reference

1

Maybe it will help someone. You can change time zone for Windows 10 using this steps:

  1. Open Settings.
  2. Click on Time & Language.
  3. Click on Date & time.
  4. Change time zone to your need.

P.s. According to the @Oscar Acevedo comment this answer is not quite correct. It's suitable for testing different time zones.

2
  • 2
    He needs to see the difference between environments server and client, because he's testing it on the same pc, so if he follows what you're saying both are going to change(that's not what he's expecting) Commented Feb 25, 2020 at 21:11
  • Yes, you are right. I understood the question incorrectly. I needed to imitate work of the app in different time zones, so I used this approach.
    – Maksim K
    Commented Feb 28, 2020 at 18:40
0

If you are into reproducibility, I suggest that you use an e2e environment, i.e. tooling that can automate browsers and allows to manipulate their time zone.

That way you won't have to fiddle around with the system time zone and you can add tests, that specifically do use different time zones, i.e. you can test your code in different time zones.

Two of the most recent popular libraries for browser automation, Puppeteer and Playwright explicitly allow setting the time zone. Using Playwright you get the ability to automate setting the time zone for all major evergreen browsers (Safari, Firefox and Chrome / ChromeEdge):

-3

You're probably better off leaving your computer timezone intact and changing your server settings to reflect a timezone ahead or behind of you. This is usually pretty easy to do depending on the server you're using.

1
  • 2
    Datetimes should be stored in UTC, changing the idea on a real server for testing a webapp is a bad idea (or even letting the system timezone affect date persistence or you app at all). Really hope you mean your "local dev server".
    – Lincoln B
    Commented May 13, 2016 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.