Here's what I made today (in 20 minutes):

[syntax="QBasic"]DECLARE FUNCTION IsValidDate% (TheDate AS STRING)

'$DYNAMIC

DEFINT A-Z

'+-----------------------------------------------------+

'| FUNCTION IsValidDate |

'| |

'| This function takes a string of 8 characters. This |

'| string indicates a specific date in the format |

'| YYYYMMDD. In which YYYY must be in the range 1800 |

'| to 3999. MM must be in the range 01 to 12. And DD |

'| must have a leading zero if DD < 10. |

'| |

'| The function returns -1 if the specific date is a |

'| valid date, otherwise it returns 0. |

'| |

'| Neo |

'+-----------------------------------------------------+

FUNCTION IsValidDate (TheDate AS STRING)

'first check for correct input (only numbers, and string must be 8 characters in length)

TD$ = LTRIM$(RTRIM$(TheDate))

Allowed$ = "0123456789"

FOR I = 1 TO LEN(TD$)

IF INSTR(Allowed$, MID$(TD$, I, 1)) = 0 THEN IsValidDate = 0: EXIT FUNCTION

NEXT I

IF LEN(TD$) <> 8 THEN IsValidDate = 0: EXIT FUNCTION

'check for the year (must be >= 1800 and <= 3999)

Year = VAL(LEFT$(TD$, 4))

IF NOT(Year >= 1800 AND Year <= 3999) THEN IsValidDate = 0: EXIT FUNCTION

'check for the month (must be >= 1 and <= 12)

Month = VAL(MID$(TD$, 5, 2))

IF NOT(Month >= 1 AND Month <= 12) THEN IsValidDate = 0: EXIT FUNCTION

'check if year Year is a leap year

IsLeapYear = ((Year MOD 4 = 0) AND (Year MOD 100 <> 0)) OR (Year MOD 400 = 0)

'prepare maxdays variable (numbers of days in the month)

I = Month

MaxDays = 30 + ((I + (I > 7)) MOD 2) + 2 * (I = 2) + (I = 2) * IsLeapYear

'check for the day (must be >= 1 and <= MaxDays)

Day = VAL(MID$(TD$, 7, 2))

IF NOT(Day >= 1 AND Day <= MaxDays) THEN IsValidDate = 0: EXIT FUNCTION

IsValidDate = -1

END FUNCTION[/syntax]

I hope it works

(I've tested it numerous times and it seems to work

)

Note that this entry was written for

*clarity*. I.e., it could have been done shorter, but I think it's much more clear this way

Another note: this entry was created in 20 minutes, without any help from online resources or previously submitted entries.

Btw, if you like this function to return "VALID" or "INVALID", you could easily make a wrapper:

[syntax="QBasic"]DECLARE FUNCTION IsValidDateWrapper$ (TheDate AS STRING)

FUNCTION IsValidDateWrapper$ (TheDate AS STRING)

IsValidDateWrapper$ = LTRIM$(MID$("IN ", 2 * ABS(IsValidDate(TheYear)) + 1, 2) + "VALID")

END FUNCTION

'note: wrapper not tested but should work

[/syntax]

Also, if you like to see what the short version looks like (the

*un*clear one):

[syntax="QBasic"]FUNCTION IsValidDate (TheDate AS STRING)

IsValidDate = 0

TD$ = LTRIM$(RTRIM$(TheDate))

FOR I = 1 TO LEN(TD$)

IF INSTR("0123456789", MID$(TD$, I, 1)) = 0 THEN EXIT FUNCTION

NEXT I

IF LEN(TD$) <> 8 THEN EXIT FUNCTION

IF VAL(LEFT$(TD$, 4)) < 1800 OR VAL(LEFT$(TD$, 4)) > 3999 OR VAL(MID$(TD$, 5, 2)) < 1 OR VAL(MID$(TD$, 5, 2)) > 12 THEN EXIT FUNCTION

I = VAL(MID$(TD$, 5, 2)) : J = VAL(LEFT$(TD$, 4))

IF VAL(MID$(TD$, 7, 2)) < 1 OR VAL(MID$(TD$, 7, 2)) > 30 + ((I + (I > 7)) MOD 2) + 2 * (I = 2) + (I = 2) * (((J MOD 4 = 0) AND (J MOD 100 <> 0)) OR (J MOD 400 = 0)) THEN EXIT FUNCTION

IsValidDate = -1

END FUNCTION[/syntax]