Convert UTC DateTime to another Time Zone

前端 未结 5 1766
渐次进展
渐次进展 2020-12-02 23:02

I have a UTC DateTime value coming from a database record. I also have a user-specified time zone (an instance of TimeZoneInfo). How do I convert that UTC DateTime to the us

5条回答
  •  天涯浪人
    2020-12-02 23:24

    Have a look at the DateTimeOffset structure:

    // user-specified time zone
    TimeZoneInfo southPole =
        TimeZoneInfo.FindSystemTimeZoneById("Antarctica/South Pole Standard Time");
    
    // an UTC DateTime
    DateTime utcTime = new DateTime(2007, 07, 12, 06, 32, 00, DateTimeKind.Utc);
    
    // DateTime with offset
    DateTimeOffset dateAndOffset =
        new DateTimeOffset(utcTime, southPole.GetUtcOffset(utcTime));
    
    Console.WriteLine(dateAndOffset);
    

    For DST see the TimeZoneInfo.IsDaylightSavingTime method.

    bool isDst = southpole.IsDaylightSavingTime(DateTime.UtcNow);
    

提交回复
热议问题