I was working on an Excel file today in an attempt to automate most of it's requirements. One cell is required to have either PDT or PST depending upon what time of year it is. I could not find anything in VB 6 that tested the system clock to determine if the time of year was currently standard or daylight time so I wrote this:
VB Code:
Sub CheckPDT() 'Holds the date for the start and end PDT Dim PDTStart As String Dim PDTEnd As String 'These hold the values days, months, year, name of the day of the week, and the number of the day of the week Dim MyDay As String Dim MyMonth As String Dim MyYear As String Dim DayNum As String Dim TestMonth As Integer Dim TestDay As Integer 'The date we are going to test Dim MyDate As String 'Hold the difference between 2 dates Dim DiffApril As Integer Dim DiffOct As Integer 'Set the initial TestMonth and TestDay for April 1 TestMonth = 4 TestDay = 1 'Get the first Sunday in April by cycling thru the days beinning from the 1st Do Until MyDay = "Sunday" MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date) MyDate = DateValue(MyDate) DayNum = Day(MyDate) MyMonth = Month(MyDate) MyMonth = MonthName(MyMonth, False) MyDay = Weekday(MyDate) MyDay = WeekdayName(MyDay, False, 1) If MyDay = "Sunday" Then PDTStart = MyMonth & " " & DayNum & ", " & Year(Date) Exit Do End If TestDay = TestDay + 1 Loop 'Reset the TestMonth, TestDay to October 31 and MyDay to nothing TestMonth = 10 TestDay = 31 MyDay = "" 'Get the last Sunday in October by cycling backwards thru the days beginning with the 31st Do Until MyDay = "Sunday" MyDate = CStr(TestMonth) & "/" & CStr(TestDay) & "/" & Year(Date) MyDate = DateValue(MyDate) DayNum = Day(MyDate) MyMonth = Month(MyDate) MyMonth = MonthName(MyMonth, False) MyDay = Weekday(MyDate) MyDay = WeekdayName(MyDay, False, 1) If MyDay = "Sunday" Then PDTEnd = MyMonth & " " & DayNum & ", " & Year(Date) Exit Do End If TestDay = TestDay - 1 Loop 'Get the difference between today's date and the start and end of Daylight Savings Time DiffApril = Val(DateDiff("d", PDTStart, Date)) DiffOct = Val(DateDiff("d", PDTEnd, Date)) 'Display the time If (DiffApril > 0) And (DiffOct < 0) Then MsgBox "The time is " & Time & " Pacific Daylight Time." Else MsgBox "The time is " & Time & " Pacific Standard Time." End If End Sub




Reply With Quote