WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Time UDFs

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Is Date Valid


Question:

I need to check if a date is valid before I pass it to the TimeAdd function. The question is, if the TimeAdd-function already has the check built-in (and it seems so because it throws an error if the date is invalid), why isn't there a separate function which checks if a date is valid ? Something like "TimeDateValid()" Perhaps a suggestion for the future ?

Answer:

Perhaps. In the meanwhile here is a UDF that should help.
#DefineFunction TimeDateValid(idate, min_year, max_year)
;This code checks for leap year
;
;Note: allowed for zeros in some of the fields, such as month,
; because the idea is to see if it was in a valid YmdHms format
; more so than a real date.
If idate == "" Then idate = "0000:00:00:00:00:00"
If StrLen(idate) == 10 Then idate = StrCat(idate,":00:00:00")
For x = 1 To 6
x%x% = ItemExtract(x,idate,":")
Next
If StrIndex(idate,":",0,@FWDSCAN) == 0 Then Return(0)

If x1 == "" Then x1 = "0000"
If x2 == "" Then x2 = "00"
If x3 == "" Then x3 = "00"
If x4 == "" Then x4 = "00"
If x5 == "" Then x5 = "00"
If x6 == "" Then x6 = "00"

If x1 < min_year || x1 > max_year Then Return(0)

If x2 < "00" || x2 > "12" Then Return(0)

If x2 == "02"
   If x1 mod 400==0
      If x3 < "01" || x3 > "29" Then Return(0)
   Else
      If x1 mod 4==0 && StrSub(x1,3,2)!='00'
         If x3 < "01" || x3 > "29" Then Return(0)
      Else
         If x3 < "01" || x3 > "28" Then Return(0)
      EndIf
   EndIf
EndIf

If (x2 == "09" || x2 == "04" || x2 == "06" || x2 == "11") Then If x3 < "01" || x3 > "30" Then Return(0)

If (x2 == "01" || x2 == "03" || x2 == "05" || x2 == "07" || x2 == "08" || x2 == "10" || x2 == "12") Then If x3 < "01" || x3 > "31" Then Return(0)

If x4 < "00" || x4 > "24" Then Return(0)
If x5 < "00" || x5 > "60" Then Return(0)
If x6 < "00" || x6 > "60" Then Return(0)

Return(1)
#EndFunction


min_year='0001'
max_year='9999'

idate='2006:09:00'
Message(idate,TimeDateValid(idate, min_year, max_year))

idate='1900:02:29'
Message(idate,TimeDateValid(idate, min_year, max_year))

idate='2000:02:29'
Message(idate,TimeDateValid(idate, min_year, max_year))

idate='2001:02:29'
Message(idate,TimeDateValid(idate, min_year, max_year))

Article ID:   W17286
File Created: 2007:07:03:14:29:20
Last Updated: 2007:07:03:14:29:20