Search code examples
vb.netdatedst

Hours in month, with daylight savings


Is there a built-in function to calculate the number of hours in a month? It needs it take care of daylightsaving (which add or reduce an hour).


Solution

  • If you're not in a time zone where midnight isn't always valid, you could do something like this (apologies if the VB syntax is slightly off):

    Dim start = New DateTime(year, month, day, 0, 0, 0, DateTimeKind.Local)
    Dim end = start.AddMonths(1)
    Dim length = end.ToUniversalTime() - start.ToUniversalTime()
    Dim hours = length.TotalHours
    

    That has a potential problem if you're somewhere like Brazil where the DST transition occurs at midnight local time. Note that the above is all assuming you want to use the system-local time zone.

    With Noda Time, you could create the appropriate LocalDate values, then convert to a ZonedDateTime at the start of the relevant day, and work out the difference that way, without any ambiguity. C# example:

    var zone = ... // Whatever DateTimeZone you want...
    var start = new LocalDate(year, month, day);
    var end = start.PlusMonths(1);
    var startInstant = zone.AtStartOfDay(start).ToInstant();
    var endInstant = zone.AtStartOfDay(end).ToInstant();
    var duration = endInstant - startInstant;
    var hours = duration.Ticks / NodaConstants.TicksPerHour;