Thursday, 12 December 2019

C#.NET - Convert DateTime from Different TimeZones to UTC

/// Takes the date time and the Zone Name of the DateTime and convert to UTC
public static DateTime ConvertSpecifiedZoneDateTimeToUTC(DateTime dateTime, string timeZoneName)
{
  return TimeZoneInfo.ConvertTimeToUtc(dateTime, TimeZoneInfo.FindSystemTimeZoneById(timeZoneName));
}
// How to Use?
DateTime utcDate= ConvertSpecifiedZoneDateTimeToUTC(DateTime.Now, "Central Standard Time")

No comments: