I need to convert DateTime+TimeZoneInfo into DateTimeOffset.
How do I do this? I assume I have to pass TimeSpan but then I'm not sure if daylight saving times will be handled properly..
Thanks!
UPDATE
TimeZoneInfo timeZone = TimeZoneInfo.FindSystemTimeZoneById("Mountain Standard Time");
return new DateTimeOffset(DateTime.UtcNow, timeZone.BaseUtcOffset);
This code throws exception..
The UTC Offset for Utc DateTime
instances must be 0.\r\nParameter
name: offset
UPDATE 2
Sorry, I didn't realize that DateTimeOffset contains only offset, it doesn't contain actual zone information – so I'm accepting answer from @Dave as it is what I will be using..
Best Solution
You should be about to get the difference between DateTime.UtcNow and DateTime.Now
If you are saving the offset, it is usually beneficial to save all dates in UTC (especially in a db) so you won't have to deal with offsets. You simply convert them before displaying but do all calculations in UTC.
Edit: If you have a TimeZone object, you can convert a UTC date to the local time for that time zone.
OR
Here's some sample code that will list a date in all timezones.