Skip to content

Instantly share code, notes, and snippets.

@davidbreyer
Last active August 29, 2015 14:27
Show Gist options
  • Save davidbreyer/1a23ca02b3ff18f6e781 to your computer and use it in GitHub Desktop.
Save davidbreyer/1a23ca02b3ff18f6e781 to your computer and use it in GitHub Desktop.
Get DateTimeOffset for a specified time zone.
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