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 - Timer and Date Functions
plus

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

Code to Check for a Valid Date

Keywords: 	 check valid date

Here is a UDF:
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsValidDateTime (strYmdHms)
intLastErrorMode = ErrorMode (@OFF)
LastError ()
TimeDiff (strYmdHms, strYmdHms)
ErrorMode (intLastErrorMode)
Return !LastError ()
;..........................................................................................................................................
; This user defined function "udfIsValidDateTime" checks a given WinBatch
; Date string e. g. "2009:02:01" or DateTime string e. g. "2009:02:01:08:00:00"
; whether it is valid or not.
; The function returns a boolean value of @FALSE (0) or @TRUE (1).
;
; Detlev Dalitz.20010325.20090724.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

blnResult11 = udfIsValidDateTime (TimeYmdHms ())         ; @TRUE.
blnResult12 = udfIsValidDateTime ("2009:02:01")          ; @TRUE.
blnResult13 = udfIsValidDateTime ("2009:02:01:23:59:59") ; @TRUE.
blnResult14 = udfIsValidDateTime ("2009:02:02:00:00:00") ; @TRUE.
blnResult15 = udfIsValidDateTime ("2009:2:1")            ; @TRUE.
blnResult16 = udfIsValidDateTime ("2009:2:1:0:0:0")      ; @TRUE.

blnResult21 = udfIsValidDateTime ("0:1:1:0:0:0")         ; @TRUE. Minimum DateTime.
blnResult22 = udfIsValidDateTime ("9999:12:31:23:59:59") ; @TRUE. Maximum DateTime.

blnResult31 = udfIsValidDateTime ("2009:02:01:00:00:60") ; @FALSE.
blnResult32 = udfIsValidDateTime ("2009:02:01:00:60:00") ; @FALSE.
blnResult33 = udfIsValidDateTime ("2009:02:01:24:00:00") ; @FALSE.
blnResult34 = udfIsValidDateTime ("2009:02:00:00:00:00") ; @FALSE.
blnResult35 = udfIsValidDateTime ("2009:00:01:00:00:00") ; @FALSE.
blnResult36 = udfIsValidDateTime ("2009:01:32:00:00:00") ; @FALSE.
blnResult37 = udfIsValidDateTime ("2009:02:29:00:00:00") ; @FALSE.
blnResult38 = udfIsValidDateTime ("2008:02:29:00:00:00") ; @TRUE. Leap-day.
blnResult39 = udfIsValidDateTime ("2008:02:30:00:00:00") ; @FALSE.

blnResult41 = udfIsValidDateTime ("2009:02:01:::")       ; @FALSE.
blnResult42 = udfIsValidDateTime ("2009:2:1:::")         ; @FALSE.

Exit


Question:

I need to check for the date entered to be a valid month and day for the year entered. Format should be MM/DD/YYYY. Typing the "/" may be required or not. The date entered should must be between 1/1/1999 and 12/31/2020.

Can you provide me with any sample code?

Answer:

Try this:

#DefineFunction IsLeapYear(yyyy) 
	return ((yyyy MOD 4 == 0) && (yyyy MOD 100 != 0)) || (yyyy MOD 400 == 0);}
#EndFunction


gotdate=@FALSE
date=""
while gotdate==@FALSE
   date=AskLine("YooHoo","Enter date in mm/dd/yyyy format",date)

   ;Three items in list?
   count=ItemCount(date,"/")
   if count!=3 then continue

   ;Get numbers
   month=ItemExtract(1,date,"/")
   day  =ItemExtract(2,date,"/")
   year =ItemExtract(3,date,"/")

   ;Are they numbers
   if !IsInt(month) then continue
   if !IsInt(day) then continue
   if !IsInt(year) then continue

   ;Are they in  correct range
   if month<1 || month>12 then continue
   if year<1999 || year>2020 then continue

   leap = IsLeapYear(year)

   if month==2 
      maxday=28+leap
   else
      if month==4 || month==6 || month==9 || month==11
         maxday=30
      else
         maxday=31
      endif
   endif

   if day<1 || day> maxday then continue

   ;normalize numbers
   month=month+0
   day=day+0
   year=year+0

   ;reconstruct date
   date=Strcat(month,"/",day,"/",year)
   gotdate=@true
endwhile

Message("A good Date",date)




 

Article ID:   W14742
Filename:   Check for a Valid Date.txt
File Created: 2012:02:17:12:21:54
Last Updated: 2012:02:17:12:21:54