5

My server's machine timezone is in HST format. When I try to get the timezone by using HTTP request in JavaScript, it is giving me in UTC format. Is there any way to get the server's timezone.

bluish
  • 26,356
  • 27
  • 122
  • 180
Maharjun M
  • 853
  • 4
  • 11
  • 24
  • Can you give more details about why you *want* to depend on the server time zone? That's almost always a bad idea. If you want to display information in a *specific* time zone, that may well be appropriate - but I would avoid using the *server's* time zone. (What if you later deploy your application elsewhere?) – Jon Skeet Feb 17 '17 at 11:24
  • We have a requirement where we have to show a warning message when user's and server's are in different timezones. Some where I read that http will give us response in UTC/GMT formats. Is there a way? – Maharjun M Feb 17 '17 at 11:30
  • 1
    That requirement sounds like a workaround for a different issue. The server's time zone should be irrelevant to how your application behaves. I would dig deeper to try to find out what you *really* need to do. – Jon Skeet Feb 17 '17 at 11:46
  • We have to show a warning message to the user as of now. – Maharjun M Feb 17 '17 at 11:57
  • 1
    But you shouldn't have to. The requirement sounds like it's hiding a bigger issue - so I suggest you address the bigger issue. – Jon Skeet Feb 17 '17 at 12:19
  • There are two feature, In the first feature we have to show a warning message, In the second we have to convert the user's timezone into server's timezone. For now our goal is to show the warning message. – Maharjun M Feb 17 '17 at 12:29

1 Answers1

9

A few things:

  • Never rely on the server's time zone to be set to anything in particular. It can easily be changed, or you may simply want to move your server somewhere else. Either should not affect your data.
  • The HTTP header will only give you the time in UTC/GMT. This part of the HTTP specification, in RFC7231 section 7.1.1.1 and 7.1.1.2.
  • The client knows nothing about the server's time zone, unless you specifically go out of your way to send it yourself. Due to the previous two points, this should not be required anyway, or should be used in very rare circumstances.
  • The server knows nothing about the client time zone either. If you want to display the value of the server's clock in the client's local time, you have two options:
  1. Send the UTC time to the client, and use JavaScript to convert from UTC to Local time. The JavaScript Date object is sufficient for this, but you may also find libraries like Moment.js and others useful.

  2. Determine the client's local time zone by some other means, either by asking the user, or by guessing. See this answer for more detail. Once you have the time zone ID (such as America/Los_Angeles, etc.) use this on the server-side. This approach is only useful if you have a lot of date/time manipulation to do on the server-side. If you are simply converting to local time for display, prefer option 1.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • Thanks. There is no way to send the server's timezone other than UTC format. So, I have to convert client's side code into UTC and Need to compare. Is it? – Maharjun M Feb 19 '17 at 09:46
  • You can send anything to the client from the server you like. It's just not something that is done for you, nor something that you should actually do. Send UTC to the client, convert UTC to client's local time on the client. Also, UTC is not a "format". – Matt Johnson-Pint Feb 20 '17 at 03:06