11

From JavaScript I have passed, to the controller, the number of minutes that the user's client date time is offset from UTC using the method getTimezoneOffset on the Date object. Now that I have this information on the server side I'd like to create a TimeZoneInfo from it. How is this possible? If this is not possible then how can I convert UTC dates on the server side into the client's timezone using the minutes offset?

Sachin Kainth
  • 45,256
  • 81
  • 201
  • 304

1 Answers1

12

I'd like to create a TimeZoneInfo from it. How is this possible?

It's not possible. A time zone offset is not the same thing as a time zone. Please read the timezone tag wiki, especially the section titled "Time Zone != Offset".

... then how can I convert UTC dates on the server side into the client's timezone using the minutes offset?

Create a DateTimeOffset that represents that moment in time. For example:

// From your database.  Make sure you specify the UTC kind.
DateTime utc = new DateTime(2013, 1, 1, 0, 0, 0, DateTimeKind.Utc);

// From JavaScript
int offsetMinutes = 420;

// Don't forget to invert the sign here
TimeSpan offset = TimeSpan.FromMinutes(-offsetMinutes);

// The final result
DateTimeOffset dto = new DateTimeOffset(utc).ToOffset(offset);

Also, make sure you understand that the offset you retrieved from the client in JavaScript is not necessarily the correct offset to apply to your database date. When you get the offset, it has to be for a particular moment in time. Since many time zones change offsets for daylight saving time, you cannot assume that the offset you currently have is appropriate for any particular value in your database. Therefore, while the above code does what you asked, it is probably still not a good idea in general.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • By the way, for some reason getTimezoneOffset does something rather strange. If you are +10 hours from UTC it will return -600 instead of 600. Do you need to update the above to take that into account? – Sachin Kainth Sep 24 '13 at 15:55
  • Regarding your last statement this is how I got the offset in the JavaScript. Is this not good enough? var d = new Date(); var minutes = d.getTimezoneOffset(); – Sachin Kainth Sep 24 '13 at 15:57
  • Already handled the inverse sign by negating the minutes when building the offset `TimeSpan`. – Matt Johnson-Pint Sep 24 '13 at 15:59
  • In the end I am looking for a DateTime object by the way. – Sachin Kainth Sep 24 '13 at 15:59
  • That will give you the *current* offset. The current offset is not guaranteed to be *always* in effect. – Matt Johnson-Pint Sep 24 '13 at 16:00
  • From `DateTimeOffset`, you can use the `.DateTime` property to get back a local `DateTime`. – Matt Johnson-Pint Sep 24 '13 at 16:01
  • Well all I need to know is what is the current time for the user. So if DST is not applied then I want 3pm but if DST is applied I want 4pm. I just want what the current time on their clock is. – Sachin Kainth Sep 24 '13 at 16:01
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/37957/discussion-between-matt-johnson-and-sachin-kainth) – Matt Johnson-Pint Sep 24 '13 at 16:01