-1

I'm simply trying to determine if a given date falls within Daylight Saving Time. In 2017, Nov 5th at 2:00 AM should be the trigger.

  • If my input is 11/5/17 00:00:00 the output is true for DST
  • If my input is 11/5/17 01:00:00 the output is false for DST

I would have expected 1 AM to be true, and 2 AM to be false for DST

here is my code

var dateTime = new DateTime(2017,11,5,0,0,0);
var targetTimeZone = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");

if (targetTimeZone.IsDaylightSavingTime(dateTime)) {
    Console.WriteLine("Daylight Saving Time");
}
stackoverfloweth
  • 6,669
  • 5
  • 38
  • 69

1 Answers1

1

From the documentation (in the Remarks section):

If the dateTime parameter specifies an ambiguous time in the current object's time zone, the TimeZoneInfo.IsDaylightSavingTime method interprets dateTime as standard time and returns false if its Kind property is DateTimeKind.Local or DateTimeKind.Unspecified. If the Kind property is DateTimeKind.Utc, this method will select the correct ambiguous time and indicate whether it is a daylight saving time.

It then goes on to explain IsAmbiguousTime, and a long example showing how to use them that very closely matches the scenario you described.

Also note that the overload for IsDaylightSavingTime that accepts a DateTimeOffset parameter does not have this issue because values cannot be ambiguous when an offset is present - similar to when Kind is UTC.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575