0

What is the best solution for converting UTC+1 to UTC in C# when you have scenario like this one:

From client side I am sending request to server. Server gets data from client and keeps it in object that has:

string Date string Time string TimeZone

properties. TimeZone property holds something like this: "Central Europe Standard Time"

Using this code:

var infos = TimeZoneInfo.GetSystemTimeZones();
var timeZone = infos.FirstOrDefault(x => x.Id == testSessionViewModel.TimeZone);
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById(testSessionViewModel.TimeZone);

I am able to find of that "Central Europe Standard Time" is actually UTC+1.

timeZone object has this value (same value has tz, those are two ways of implementing same thing, for testing purpose):

enter image description here

Now I want to use this information in some way to convert UTC+1 time that was result to UTC.

My idea is:

  1. Take date and time strings and concatenat them to one string.
  2. Parse that string to DateTime
  3. Somehow get +1 value (possible from BaseUtcOffset propery)

enter image description here

  1. Substract 1 hour to get UTC time.

This looks pretty messy to me. So my question is did someone done something like this before and has some better idea or practice.

Other tought was: instead of sending "Central Europe Standard Time" from client to server and doing all that conversion thing, looking for time zone etc. Maybe i can send sting 1, when I have selected UTC+1, and convert it to some number type, and directly use that value when adding or substracting time.

I am open for more ideas :)

nemo_87
  • 4,523
  • 16
  • 56
  • 102
  • all you need is `dt.ToUniversalTime()` – Alfie Goodacre Nov 01 '16 at 14:52
  • "I am able to find of that "Central Europe Standard Time" is actually UTC+1." Well, it is when it's not observing DST. The Windows time zone IDs are really unfortunate - that ID is used for time zones which observe DST too. – Jon Skeet Nov 01 '16 at 14:53
  • Use DateTime.ToUniversalTime() on the client to get the universal time on the client, then send that value to the server. – NineBerry Nov 01 '16 at 14:53
  • 3
    Programmers in England are not blessed with their timezone. A UTC time is the same anywhere in the world. – Hans Passant Nov 01 '16 at 14:56

0 Answers0