By using HTTP request how to get server's timezone not in UTC format

16,645

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.

Share:
16,645
Maharjun M
Author by

Maharjun M

I am working with Walmart Labs now. I am interested in learning new things and new technologies

Updated on June 08, 2022

Comments

  • Maharjun M
    Maharjun M almost 2 years

    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.

  • Maharjun M
    Maharjun M over 7 years
    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?
  • Matt Johnson-Pint
    Matt Johnson-Pint over 7 years
    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".