Easy Way

VBScript to determine if date is in DST

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


'Code begins here

'IsDST.vbs
'=======================================
Function IsDST(TodayDate, arrReturn)
' Is any date in DST and dates DST begins and ends
' Args = Date, 1 dimmensional 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

© 2002 by Paul R. Sadowski
All Rights Reserved. Used By Permission.
Comments to: aa089@bfn.org