Getting Daylight Savings Time Start and End in NodaTime

前端 未结 2 761
旧巷少年郎
旧巷少年郎 2020-12-06 02:00

How can I get the starting and ending dates for Daylight Savings Time using Noda Time? The function below accomplishes this task but it is horribly unwieldy and is begging f

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-06 02:38

    This snippet code also help you to check a time is in daylightsavingstime or not

    public static bool IsDaylightSavingsTime(this DateTimeOffset dateTimeOffset)
            {
                var timezone = "Europe/London"; //https://nodatime.org/TimeZones
                ZonedDateTime timeInZone = dateTimeOffset.DateTime.InZone(timezone);
                var instant = timeInZone.ToInstant();
                var zoneInterval = timeInZone.Zone.GetZoneInterval(instant);
                return zoneInterval.Savings != Offset.Zero;
            }
    

    how to use it

    var testDate = DateTimeOffset.Now;
    var isDst = testDate.IsDaylightSavingsTime();
    

    Depend on your situation, you can modify it a bit

提交回复
热议问题