Thursday 12 December 2019

C# - How to get CST to UTC DateTimeOffset?

DateTimeOffset - Gives detailed date and time difference.
/// <summary>
/// Gets the datetime offset difference between the CST to UTC
/// </summary>
/// <returns></returns>
public static DateTimeOffset GetCstToUtcOffset()
{
    TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
    DateTimeOffset utcOffset = new DateTimeOffset(DateTime.UtcNow, TimeSpan.Zero);
    return utcOffset.ToOffset(cstZone.GetUtcOffset(utcOffset));
}

No comments: