# Daylight Savings Time-determining the dates when the switch occurs

I was wondering if there were any vb functions or tricks to finding out the day the time changes in a given year.

In my program I need to know which day the time changes so my program can adapt its routines to account for the time change in its calculations.

if someone knows a quick way to do this that would be great.

Kelly
LVL 2
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
For the states which use daylight savings time, it starts at midnight on the first sunday in april, and ends at midnight on the last sunday in october.

0
Commented:
These functions return the start and end date for daylight savings time.

Private Function FirstSunApril(p_lYear As Long) As Date

Dim d1stOfMonth As Date
d1stOfMonth = CDate("4/1/" & p_lYear)

Dim d1stSunday  As Date
If Weekday(d1stOfMonth) = 1 Then
d1stSunday = d1stOfMonth
Else
d1stSunday = DateAdd("d", 8 - Weekday(d1stOfMonth), d1stOfMonth)
End If

FirstSunApril = d1stSunday

End Function

Private Function LastSunOct(p_lYear As Long) As Date

Dim dLastOfMonth As Date
dLastOfMonth = CDate("10/31/" & p_lYear)

Dim dLastSunday  As Date
If Weekday(dLastOfMonth) = 1 Then
dLastSunday = dLastOfMonth
Else
dLastSunday = DateAdd("d", -1 * (Weekday(dLastOfMonth) - 1), dLastOfMonth)
End If

LastSunOct = dLastSunday

End Function
0
Commented:
You use this

Replace 2003 to any year you want

Option Explicit

Private Sub Command1_Click()
Debug.Print CalcDayLightBoundary(2003, 4, 1)
Debug.Print CalcDayLightBoundary(2003, 10, 2)
End Sub
Private Function CalcDayLightBoundary(Yr As Integer, Mon As Integer, Hr As Integer) As Date
Dim tt As Date, NumDays As Integer

On Error Resume Next
tt = DateSerial(Yr, Mon, 1) + TimeSerial(Hr, 0, 0)
NumDays = IIf(Weekday(tt) = 1, 0, 7 - Weekday(tt) + 1)
If (Mon = 10) Then
NumDays = NumDays + 28
If (NumDays > 31) Then NumDays = NumDays - 7
End If
End Function
0

Experts Exchange Solution brought to you by