I'm adding a function to see if the current day lands during Daylight savings time.
How do you get the dates for second Sunday in March and the first Sunday in November of the current year?
Printable View
I'm adding a function to see if the current day lands during Daylight savings time.
How do you get the dates for second Sunday in March and the first Sunday in November of the current year?
Would this sample be what you're after?
Like this:
Code:Dim DSTon As Date, DDSoff As Date, DOW As Integer
DOW = (8 - WeekDay(DateSerial(Year(Date), 3, 1))) Mod 7
DSTon = DateSerial(Year(Date), 3, 1 + DOW + 7)
DOW = (8 - WeekDay(DateSerial(Year(Date), 11, 1))) Mod 7
DSToff = DateSerial(Year(Date), 11, 1 + DOW)
Time should be considered as well:
Code:Function InDST(Optional ByVal DateTime As Date, _
Optional ByRef dstStart As Date, _
Optional ByRef dstEnd As Date) As Boolean
If DateTime = 0 Then DateTime = Now
dstStart = DateSerial(Year(DateTime), 3, 1) + #2:00:00 AM#
dstStart = dstStart + 14 - Weekday(dstStart, vbMonday) '-- 2nd Sunday in March
dstEnd = DateSerial(Year(DateTime), 11, 1) + #2:00:00 AM#
dstEnd = dstEnd + 7 - Weekday(dstEnd, vbMonday) '-- 1st Sunday in November
If (DateTime >= dstStart) Then If (DateTime < dstEnd) Then InDST = True
End Function
Sub TestDST()
Dim dstStart As Date, dstEnd As Date, dt As Date
Debug.Print Now, InDST()
Debug.Print Now, InDST(, dstStart, dstEnd), dstStart, dstEnd
dt = #3/9/2008 1:30:00 AM#
Debug.Print dt, InDST(dt)
dt = #3/9/2008 5:30:00 AM#
Debug.Print dt, InDST(dt)
dt = #3/9/2009#
Debug.Print dt, InDST(dt, dstStart, dstEnd), dstStart, dstEnd
End Sub
Thanks Rino, but it looks like the start of DST has been moved from the 1st sunday in march to the 2nd.Quote:
Originally Posted by RhinoBull
http://aa.usno.navy.mil/faq/docs/daylight_time.php
The VBnet code isn't getting it correctly.
Thx technorobbo, I knew there had to be a better way than a loop.
And thank you anhn, you got it all there :)