4

I need to create a DateTime with a set date and time which will be in a specific time zone(West Asia Standard Time, W. Europe Standard Time etc).

DST must be preserved, so offset is out because for half of the year for a given time zone it will be for example +2h and for the other half +3h.

Then I want to convert the date to UTC.

I tried to do it in such a way that I could add this timezone offset later. However, firstly I do not like this solution and I am afraid that I will lose one hour when the time changes twice a year, and secondly I get an error:

"The UTC Offset for Utc DateTime instances must be 0."

var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, DateTimeKind.Utc);
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var timezoneTime = TimeZoneInfo.ConvertTimeFromUtc(runTime, timeZoneInfo);
var offset = timeZoneInfo.GetUtcOffset(timezoneTime);

I would like to get this kind of code

var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0, timeZoneInfo);
var utcTime = testTime.ToUniversalTime();

So, to sum up, I want to have method, where I pass year, month, day, hour, minute and timeZone and in return I will get DateTime that is in UTC In javascript there are libraries, where the given time zone is given as a parameter but I have to do it on the server side.

6
  • The timezone can be different for each request and DST must be preserved, so offset is out. I don't understand this statement. Please explain in more detail why offset is out. When you say offset what do you mean exactly? Do you mean the DateTimeOffset type (which, frankly, is almost certainly certainly what you should be using)? Or something else? Commented Jun 27, 2021 at 12:19
  • Please share a minimal reproducible example. Be really clear what the input is and what exact output you want. Your description of the problem is unclear what outcome you want and what steps you are taking to try and get that outcome. What we really want to know is what exact output you want - just in case you are attacking the problem the wrong way. Commented Jun 27, 2021 at 12:20
  • I want to have method, where I pass year, month, day, hour, minute and timeZone and in return I will get DateTime that is in UTC. A fixed offset is not suitable, because for half of the year for a given time zone it will be +2h and for the other half +3h. Commented Jun 27, 2021 at 12:36
  • Put it in your question. Actual code with actual values and specify the exact output you want. Preferably a couple of examples, on either side of the DST boundary. Commented Jun 27, 2021 at 12:40
  • 1
    Also, have you tried DateTimeOffset or NodaTime? Did they work? Commented Jun 27, 2021 at 12:41

1 Answer 1

8

You'd basically need TimeZoneInfo.ConvertTimeToUtc method.

Just make sure the Kind property of the passed DateTime is Unspecified, otherwise the method has special expectations for the sourceTimeZone argument and will throw exception.

e.g.

var testTime = new DateTime(testDate.Year, testDate.Month, testDate.Day, 4, 0, 0);
var timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("West Asia Standard Time");
var utcTime = TimeZoneInfo.ConvertTimeToUtc(testTime, timeZoneInfo);;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.