0

I am trying do to something like this:

On a client side I have datepicker for selecting date, drop down for selecting an hour, and drop down with time zones for selecting user time zone.

I am sending this info to server. On a server side, I want to accomplish this:

Take date and time values, and check what is the value of time zone. If it is for example "UTC+1" (or any other +- value of UTC time), convert that into UTC, before saving.

What I am not sure how to do it is: What value should I send from client as time zone information so server can detect it is a for example UTC+1.

I saw examples like this:

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); 

On how to find out what is the time zone by its id, but I cannot do something like this:

TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("UTC+1");  

Because, I get an exception, of course:

Additional information: The time zone ID 'UTC+1' was not found on the local computer.

Does someone know what is Id for all UTC+-someNumber, or there is a way to detect timezone from UTC string in some different way?

Did someone had experience whit this kind of a conversion?

nemo_87
  • 4,523
  • 16
  • 56
  • 102
  • You can get *all the time zones about which information is available on the local system* by calling `TimeZoneInfo.GetSystemTimeZones()` and check their `BaseUtcOffset`. Besides that, it might be simpler to calculate the UTC time on the client side, before sending it to the server. – Clemens Nov 01 '16 at 11:14

2 Answers2

1

As far as I remember the only way to work with the timezones is by the full name that is available in your OS.

You can find a similar question here.

A list from MSDN is here.

So there seems to be no easy way to convert strings like "UTC+1", unless you create a mapping to the ones that are supported by framework.

Community
  • 1
  • 1
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
0

You could use TimeZoneInfo.CreateCustomTimeZone():

DateTime utcTime = DateTime.UtcNow;
TimeZoneInfo targetTimeZone = TimeZoneInfo.CreateCustomTimeZone("MyId", TimeSpan.FromHours(1), "Somewhere", "Somewhere");
DateTime targetTime = TimeZoneInfo.ConvertTime(utcTime, targetTimeZone);

or you can use DateTimeOffset, or DateTime.AddHours(1):

var datetimeOffset = new DateTimeOffset(yourDateTime, TimeSpan.FromHours(1));
var otherDatetime = yourDateTime.AddHours(1);
Danh
  • 5,916
  • 7
  • 30
  • 45