35

I am using C#. I need to get the local time zone info for the person running a web application.

I was wondering if:

TimeZoneInfo tzinfo = TimeZoneInfo.Local;
TimeZoneInfo.ConvertTimeFromUtc(result.DueDate.Value, tzinfo);

is appropriate. Again depending on what time zone the person running the application is, I would like that to be reflected.

Larry Tang
  • 642
  • 5
  • 23
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 8
    You said "web application" - are you trying to get the time zone of the *server* or the *client*? – Dan J Aug 07 '13 at 19:32
  • 2
    I am trying to get the timezone of the client. – Nate Pet Aug 07 '13 at 20:29
  • Here's how I convert a UTC DateTime into a user's local time for a website. The trick is to get the user (client) to tell the server which timezone they're in, and let the server offset the UTC DateTime into their local time: https://stackoverflow.com/questions/8194016/how-to-get-current-user-timezone-in-c-sharp/30503614#30503614 – Mike Gledhill Jul 26 '17 at 08:28

1 Answers1

39

yes you are right, this is what you should use. Alternative can be

TimeZone localZone = TimeZone.CurrentTimeZone;

but the CurrentTimeZone property corresponds to the TimeZoneInfo.Local property so no difference really.

it displays the names for standard time and daylight saving time for the local time zone.

Ehsan
  • 31,833
  • 6
  • 56
  • 65
  • 8
    You may want to clarify that this returns the local time zone of the machine hosting the process and not necessarily the "person who is running the web application". – Mayo Aug 07 '13 at 19:34
  • Thanks. I am using TimeZoneInfo.ConvertTimeFromUtc on the edited vesion above. Please let me know if this is the best approach. Thank you. – Nate Pet Aug 07 '13 at 19:34
  • you want cliens time zone info or server? – Ehsan Aug 07 '13 at 19:37
  • I need to get the user's time zone.. So it will NOT be the time zone on the server. Please advise. Thanks – Nate Pet Aug 07 '13 at 19:50
  • 2
    it won't be possible without javascript, see http://www.c-sharpcorner.com/uploadfile/vendettamit/setting-your-website-datetime-according-to-time-zone-offset-of-the-client/ – Ehsan Aug 07 '13 at 19:55
  • 14
    @NatePet You need to unmark this as the answer. You asked if this will give you a web client's timezone and this answer says, "yes", which is incorrect. This code gives you the web server's time zone. – xr280xr May 13 '16 at 15:28
  • TimeZone is not the same as TimeZoneInfo so although one could argue that CurrentTimeZone 'corresponds' to the TimeZoneInfo.Local, there is a difference. Regardless, the only way to get the timeZone on the client is for the client to post it to you explicitly. The link from Ehsan provides good info. – Rob Von Nesselrode Jan 17 '18 at 21:39
  • Why is this answer marked as correct answer? It doesn't provide the timezone of person running the application but the server where your website/app is hosted. – vohrahul Jun 07 '18 at 14:29
  • 2
    Yes, Why the answer marked as correct answer? It's giving the server machine timezone. – Umesh Sehta Sep 13 '18 at 13:27
  • 1
    From the [docs](https://learn.microsoft.com/en-us/dotnet/api/system.timezone.currenttimezone), prefer `TimeZoneInfo.Local` over `TimeZone.CurrentTimeZone`. It states the reason. – nawfal Sep 20 '18 at 09:20