Check if daylight savings is in effect?

前端 未结 8 1779
悲哀的现实
悲哀的现实 2020-12-29 02:14

How to check if in Denmark daylight time savings has taken effect, if so, then add 1 hour to my data, else not? I have a xml file:



        
相关标签:
8条回答
  • 2020-12-29 03:18

    When I coded as above - for New-York, I found in the debugger that the time was set correctly (including DST)

    TimeZoneInfo nyTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time");
    
    DateTime nyTime = GetLocalDateTime(DateTime.UtcNow, nyTimeZone);
    
    if (nyTimeZone.IsDaylightSavingTime(nyTime))
        nyTime = nyTime.AddHours(1);
    
    public static DateTime GetLocalDateTime(DateTime utcDateTime, TimeZoneInfo timeZone)
        {
    
            utcDateTime = DateTime.SpecifyKind(utcDateTime, DateTimeKind.Utc);
    
            DateTime time = TimeZoneInfo.ConvertTime(utcDateTime, timeZone);
    
            return time;
    
        }
    
    0 讨论(0)
  • 2020-12-29 03:18

    this is my short solution which can be using in all timezones:

    DateTime utcTime = DateTime.Parse("30.10.2018 18:21:34")
    DateTime localtime = ConvertUTCToLocalTime(utcTime);
    
    
    public static DateTime ConvertUTCToLocalTime(DateTime UTCTime)
    {
        var localZone = TimeZone.CurrentTimeZone;
        var offset = localZone.GetUtcOffset(UTCTime);
        var localTime = UTCTime.AddHours(offset.Hours);
        return localTime;
    }
    
    0 讨论(0)
提交回复
热议问题