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):
Now I want to use this information in some way to convert UTC+1 time that was result to UTC.
My idea is:
- Take date and time strings and concatenat them to one string.
- Parse that string to DateTime
- Somehow get +1 value (possible from BaseUtcOffset propery)
- 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 :)