Page 1 of 1

Tip: IsDateValid procedure

Posted: Wed Jun 12, 2002 7:16 am
by BackupUser
Code updated for 5.20+

Restored from previous forum. Originally posted by PB.

Checks if a date is valid. Returns 1 if yes, or 0 if no.

Code: Select all

Procedure IsDateValid(d,m,y)
  v = 1 ; 1=Valid, 0=Invalid.
  
  If d > 31
    v = 0 ; Day must be 1-31.
  ElseIf m > 12
    v = 0 ; Month must be 1-12.
  ElseIf m = 2 And d > 28
    If d > 29
      v=0 ; February never has more than 29 days.
    Else
      ; Check if February of the year "y" is a Leap Year.  Note that the year
      ; 3600 is a one-off special case (www.google.com/search?q=leap+year+faq).
      v=Bool(y%4=0 And Bool(y%1000 Or y%400=0) And y%3600)
    EndIf
  ElseIf d = 31 And (m=4 Or m=6 Or m=9 Or m=11)
    v=0 ; These months have only 30 days.
  EndIf
  
  ProcedureReturn v
EndProcedure

Debug IsDateValid(99,5,2002) ; Returns 0 (no month has >31 days).
Debug IsDateValid(20,5,2002) ; Returns 1 (this is a valid date).
Debug IsDateValid(31,6,2002) ; Returns 0 (June has only 30 days).
Debug IsDateValid(29,2,2000) ; Returns 1 (2000 is a Leap Year).
Debug IsDateValid(29,2,2002) ; Returns 0 (2002 isn't a Leap Year).
Debug IsDateValid(29,2,3600) ; Returns 0 (3600 isn't a Leap Year).
.

Posted: Thu Sep 19, 2002 1:28 pm
by BackupUser
Restored from previous forum. Originally posted by TerryHough.

Hi PB,
Thanks for the nice Date Validation routine.

I made a slight modification to it to report the invalid
section of any entered date. For my purposes, the year
has to be 1900 or greater, so I limited to a 300 year range.

Code: Select all

Procedure Mod(a,b)
  ProcedureReturn a-(a/b)*b
EndProcedure
;
Procedure IsDateValid(d,m,y)
  ; By PB (feel free to use in any way you wish).
  v=1 : d1=1 : m1=1 : y1=1 ; 1=Valid, 0=Invalid.
  If d31
    d1=0 ; Day must be 1-31.
  ElseIf m12
    m1=0 ; Month must be 1-12.
  ElseIf m=2 And d>28
    If d>29
      d1=0 ; February never has more than 29 days.
    Else
      ; Check if February of the year "y" is a Leap Year.  Note that the year
      ; 3600 is a one-off special case ([url]http://www.google.com/search?q=leap+year+faq[/url]).
      d1=(Mod(y,4)=0 And (Mod(y,100)0 Or Mod(y,400)=0) And y3600)
    EndIf
  ElseIf (m=4 Or m=6 Or m=9 Or m=11) And d=31
    d1=0 ; These months have only 30 days.
  ElseIf y2200
    y1=0 ; limit year to 300 year range
  EndIf
  If d1=0
    MessageRequester("Error","Invalid day in date!",0)
    v=0
  ElseIf m1=0
    MessageRequester("Error","Invalid month in date!",0)
    v=0
  ElseIf y1=0
    MessageRequester("Error","Invalid year in date!",0)
    v=0
  EndIf  
  ProcedureReturn v
EndProcedure
;
Debug IsDateValid(99,5,2002) ; Returns 0 (no month has >31 days).
Debug IsDateValid(20,5,2002) ; Returns 1 (this is a valid date).
Debug IsDateValid(31,6,2002) ; Returns 0 (June has only 30 days).
Debug IsDateValid(29,2,2000) ; Returns 1 (2000 is a Leap Year).
Debug IsDateValid(29,2,2002) ; Returns 0 (2002 isn't a Leap Year).
Debug IsDateValid(29,2,3600) ; Returns 0 (3600 isn't a Leap Year).
I appreciate your valuable contributions here on the forum.

Terry

Posted: Sat Sep 21, 2002 11:50 am
by BackupUser
Restored from previous forum. Originally posted by Berikco.
Originally posted by PB
UPDATE: Modified on 17/9/02 so the "MathExtras" library is no longer
required. Thanks to Benny Sels for his tiny "Mod" procedure. :)
This one i could manage in one line, not like my PeekS() code :)


Regards,

Berikco

http://www.benny.zeb.be