-5

how to get user local machine time zone in c#. I'm trying to get time by using

var PublishedOn = DateTime.Now;

but time shows according to the server not local user machine.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
ALi Moazzam
  • 18
  • 1
  • 6

3 Answers3

2

The only place where you can get such information reliably is the client machine. Javascript would be the way to go.

Geo-location will not work when behind a proxy and is generally not very reliable (in my experience).

Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
0

DateTime.Now will be the machine where the code is executed. You can use the IP of the incoming request:

Request.UserHostAddress

to get some idea of the geo location using an API like

https://freegeoip.net

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Tom Elmore
  • 1,980
  • 15
  • 20
-5

TimeZone.CurrentTimeZone is used for the time zone on the computer where the code is executing. Check these links: http://msdn.microsoft.com/en-us/library/system.timezone.currenttimezone.aspx

http://msdn.microsoft.com/en-us/library/system.timezoneinfo.local.aspx

  • 1
    How is this the correct answer? this will still give the server Timezone *not* the local pcs – Jeremy Thompson Mar 14 '16 at 11:24
  • 1
    Not only that, but `TimeZone` should not be used at all anymore. It was replaced by `TimeZoneInfo` a long time ago. MSDN is clear on that. Still, it won't help for the requested function, as it requires JavaScript. – Matt Johnson-Pint Mar 15 '16 at 05:42