Last active
August 29, 2015 14:27
-
-
Save davidbreyer/1a23ca02b3ff18f6e781 to your computer and use it in GitHub Desktop.
Get DateTimeOffset for a specified time zone.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static DateTimeOffset GetCurrentDateTimeForTimeZone(string TimeZoneStandardName) | |
{ | |
try | |
{ | |
//Find specified time zone | |
var timeZone = TimeZoneInfo.FindSystemTimeZoneById((TimeZoneStandardName)); | |
//Convert UTC time to a specified local time. | |
DateTime localTime = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, timeZone); | |
//Create a date time offset with the local time. | |
DateTimeOffset localTimeWithOffset = new DateTimeOffset(localTime, timeZone.GetUtcOffset(localTime)); | |
return localTimeWithOffset; | |
} | |
// Handle exception if time zone is not defined in registry | |
catch (TimeZoneNotFoundException tze) | |
{ | |
throw tze; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment