http://webexhibits.org/daylightsaving/g.html
How can my VB determine the 1st sunday, or the last without being big/ugly?
Perhaps someone has a nice elegant way?
You can use the Year function to get the year from a given date. Also, you
can use +/- instead of DateAdd in both functions if you only care about
dates after 1900. So, for a given date, DST in the USA starts on
FirstDayInMonth(Year(testDate), 4, vbSunday) and ends on
LastDayInMonth(Year(testDate), 10, vbSunday)
-Matt
"FN" <newsgroupYESDE...@DELETECAPSyahoo.com> wrote in message
news:DXBC9.18463$%k2.54...@twister.socal.rr.com...
Dim checkDate As Date
checkDate = DateValue(Text1.Text)
If checkDate >= DateSerial(Year(checkDate), 4, 1 _
- Weekday(DateSerial(Year(checkDate), 4, 1), vbMonday) + 7) And _
checkDate <= DateSerial(Year(checkDate), 11, 1 _
- Weekday(DateSerial(Year(checkDate), 11, 1), vbMonday)) Then
MsgBox "daylight savings time"
Else: MsgBox "normal time"
End If
Please note that daylight saving time varies in different countries.
E.g. European Union has its own, as well as United Kingdom (England).
Both differ from the one used in USA.
br kgb
Oi! UK <> England.
The relevant timezone isn't just the UK either, as it includes Eire
(Ireland, if you prefer), and we don't call it DST.
Other than that, spot on. ;-)
Adam
--
Work expands to fill the time available.
-- Cyril Northcote Parkinson, "The Economist", 1955
This is still not exactly what you're after, but it might give you some ideas.
The last day of DST I've called "normal time"
In theory, if you click on a country, enter a date, hit command1, you'll get a message whether it's DST or normal time
(it's extremely raw - so may require a tweak or two)
What this needs is
list1 - sorted (to display countries)
text1 (to enter a date to check)
command1 (go button)
text2 (country name to add to list1)
combo1 to 6 (style 2 - dropdown)
(group in 3's, 1-3 for DST start, 4-6 for DST end)
command2 (add new details)
Option Explicit
Private Data(1000, 7) As String
'----------------------------------
Private Sub Form_Load()
Dim i As Integer
Combo1.AddItem "1st": Combo1.AddItem "2nd"
Combo1.AddItem "3rd": Combo1.AddItem "4th"
Combo1.AddItem "Last": Combo4.AddItem "1st"
Combo4.AddItem "2nd": Combo4.AddItem "3rd"
Combo4.AddItem "4th": Combo4.AddItem "Last"
For i = 1 To 12
If i < 8 Then
Combo2.AddItem Format(DateSerial(6, 1, i), "dddd")
Combo5.AddItem Format(DateSerial(6, 1, i), "dddd")
End If
Combo3.AddItem Format(DateSerial(1, i, 1), "mmmm")
Combo6.AddItem Format(DateSerial(1, i, 1), "mmmm")
Next
LoadListBox
End Sub
'--------------------------------
Private Sub Command1_Click()
If List1.ListIndex = -1 Then MsgBox "no country": Exit Sub
Dim checkDate As Date
Dim num As Integer
Dim firstDOWfrom As Integer
Dim firstDOWto As Integer
Dim mthFrom As Integer
Dim mthTo As Integer
Dim yrFrom As Integer
Dim yrTo As Integer
checkDate = DateValue(Text1.Text)
num = List1.ItemData(List1.ListIndex)
firstDOWfrom = (Val(Data(num, 2)) + 1) Mod 8
If firstDOWfrom = 0 Then firstDOWfrom = 1
firstDOWto = (Val(Data(num, 5)) + 1) Mod 8
If firstDOWto = 0 Then firstDOWfrom = 1
mthFrom = Val(Data(num, 3))
yrFrom = Year(checkDate)
If Month(checkDate) < mthFrom Then yrFrom = yrFrom - 1
If Data(num, 1) = 5 Then mthFrom = mthFrom + 1
mthTo = Val(Data(num, 6))
If Data(num, 4) = 5 Then mthTo = mthTo + 1
yrTo = Year(checkDate)
If mthFrom > mthTo Then yrTo = yrFrom + 1
If checkDate >= DateSerial(yrFrom, mthFrom, 1 _
- Weekday(DateSerial(yrFrom, mthFrom, 1), firstDOWfrom) _
+ (Data(num, 1) Mod 5) * 7) And _
checkDate <= DateSerial(yrTo, mthTo, 1 _
- Weekday(DateSerial(yrTo, mthTo, 1), firstDOWto) _
+ (Data(num, 4) Mod 5) * 7 - 1) Then
MsgBox "daylight savings time"
Else: MsgBox "normal time"
End If
End Sub
'---------------------------------------
Private Sub Command2_Click()
Dim ff As Long: ff = FreeFile()
Open App.Path & "\DST.txt" For Append As #ff
Write #ff, Text2.Text, Val(Combo1.ListIndex) + 1, _
Val(Combo2.ListIndex) + 1, Val(Combo3.ListIndex) + 1, _
Val(Combo4.ListIndex) + 1, Val(Combo5.ListIndex) + 1, _
Val(Combo6.ListIndex) + 1
Close #ff
LoadListBox
End Sub
'---------------------------
Private Sub LoadListBox()
Dim ff As Long: ff = FreeFile()
Dim i As Integer
List1.Clear
If Len(Dir(App.Path & "\DST.txt")) > 0 Then
Open App.Path & "\DST.txt" For Input As #ff
Do While Not EOF(ff)
Input #ff, Data(i, 0), Data(i, 1), Data(i, 2), _
Data(i, 3), Data(i, 4), Data(i, 5), Data(i, 6)
List1.AddItem Data(i, 0)
List1.ItemData(List1.NewIndex) = i
i = i + 1
Loop
Close #ff
End If
End Sub
It's just an example
Paste the following into your project. It will tell you if Daylight
Savings Time is currently engaged on the computer. Simply call the
function like this
MsgBox IsDST
There is the possibility for GetTimeZoneInformation to return an UNKNOWN
condition; I treat this as a False for the purposes of this function.
Rick - MVP
Private Declare Function GetTimeZoneInformation _
Lib "kernel32" _
(lpTimeZoneInformation As _
TIME_ZONE_INFORMATION) As Long
Private Type SYSTEMTIME
wYear As Integer
wMonth As Integer
wDayOfWeek As Integer
wDay As Integer
wHour As Integer
wMinute As Integer
wSecond As Integer
wMilliseconds As Integer
End Type
Private Type TIME_ZONE_INFORMATION
Bias As Long
StandardName(31) As Integer
StandardDate As SYSTEMTIME
StandardBias As Long
DaylightName(31) As Integer
DaylightDate As SYSTEMTIME
DaylightBias As Long
End Type
Public Function IsDST() As Boolean
Dim TZInfo As TIME_ZONE_INFORMATION
Const TIME_ZONE_ID_DAYLIGHT As Long = 2
If GetTimeZoneInformation(TZInfo) = _
TIME_ZONE_ID_DAYLIGHT Then IsDST = True
End Function
For example, the US was on "War Time" during WW2, essentially year-round
DST. On exactly what dates did this switch in and out?
Also, the present US scheme of first Sunday in April / last Sunday in
October is only about 30 years old -- used to be the last Sunday in
April. Before that, DST was not universally observed, and even now
there are whole states, and some individual counties, that never observe
DST. At least one town straddles such a border, and they must keep two
sets of clocks to know the time on opposite sides of the street.
--
Jim Mack
MicroDexterity Inc
www.microdexterity.com
"FN" wrote...