Wilson WindowWare Tech Support

WinBatch WinBatch+Compiler WebBatch
Home | Tech Database | Tech BBS | White Papers | Purchase


Code to Check for a Valid Date

Keywords: 	 check valid date

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:
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

   if (year mod 4) == 0 then leap=1
   else leap=0

   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