The function IsDST determines if any date is
in the Daylight Savings Time
period as defined in the United States. To use this script
for other
countries you will have to make appropriate adjustments for
your locale.
The function returns a value of 0 for not in DST; 1 for in
DST
and -1 if the date argument supplied is invalid.
The function requires two arguments:
A string date value
A one-dimensional two element array
The one-dimensional two element array returns two strings,
the date DST begins in
the year specified in the date value, and the date DST ends
for that same
year. The purpose of composing the script in this manner is
to allow you
to not only determine if any date is within DST but also to
allow you to
query the script with a date value to discover the range of
DST dates for
any given year (to calculate, for example, how many days
until DST begins
or ends). If this feature does not interest you simply
ignore the values
returned in the array; however you must pass it a
one-dimensional two element array
in any case.
Sample usage:
option explicit
Dim arrDST(1), x
x = IsDST(Date, arrDST)
wscript.echo x, arrDST(0), arrDST(1)
Sample output:
0 4/7/2002 10/27/2002
2007 rules compatabile version provided by Dan Gould.
(Thanks Dan!)
|
' isDST() - Returns 1 if in DST, 0 if not, -1 on
bad date
' - argDate: Today's date
' - argReturn: Two element array
containing DST start and end dates
Function
isDST(argDate, argReturn)
Dim StartDate, EndDate
If (Not IsDate(argDate)) Then
argReturn(0) = -1
argReturn(1) = -1
isDST = -1
Exit Function
End If
' DST start date...
StartDate = DateSerial(Year(argDate), 3, 1)
Do While (WeekDay(StartDate) <> vbSunday)
StartDate = StartDate + 1
Loop
StartDate = StartDate + 7
' DST end date...
EndDate = DateSerial(Year(argDate), 11, 1)
Do While (WeekDay(EndDate) <> vbSunday)
EndDate = EndDate + 1
Loop
'
Finish up...
isDST = 0
If ((argDate >= StartDate) And (argDate <
EndDate)) Then
argReturn(0) = StartDate
argReturn(1) = EndDate
isDST = 1
End If
End Function
|
'Code begins here
(Pre-2007 Version)
'IsDST.vbs
'=======================================
Function IsDST(TodayDate, arrReturn)
' Is any date in DST and dates DST begins and ends
' Args = Date, 1 dimensional two element array
' Returns -1 on error (bad date)
' 0 if NOT DST
' 1 If in DST
' -1 If date value is invalid
' arrRetunr(0) = First day of DST
' arrReturn(1) = Last day of DST
' Paul R. Sadowski <aa089(at)bfn.org>
Dim StartDate, EndDate, StartDOW, EndDOW, TargetDOW
Dim BeginDST, EndDST
if IsDate(TodayDate) <> True then
arrReturn(0) = -1
arrReturn(1) = -1
IsDST = -1
Exit Function
end if
StartDate = CDate("4/1/" & Cstr(Year(TodayDate)))
EndDate = CDate("11/1/" & Cstr(Year(TodayDate)))
StartDOW = Weekday(StartDate)
if StartDOW <> 1 then
TargetDOW = 8 - StartDOW
end if
BeginDST = DateAdd("d", TargetDOW, StartDate)
EndDOW = Weekday(EndDate)
if EndDOW <> 1 then
TargetDOW = 1 - EndDOW
end if
EndDST = DateAdd("d", TargetDOW, EndDate)
if DateDiff("d", BeginDST, TodayDate) >= 0 then
if DateDiff("d", EndDST, TodayDate) < 0 then
arrReturn(0) = BeginDST
arrReturn(1) = EndDST
IsDST = 1
Exit Function
end if
end if
arrReturn(0) = BeginDST
arrReturn(1) = EndDST
IsDST = 0
End Function |
|
© 2003 by Paul R. Sadowski
All Rights Reserved. Used By Permission.
Comments to: Scripting
|