Best way to validate a string is a YYYYMMDD

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,602
46
Albuquerque, NM
prospero.unm.edu
#6
@ISDATE will tell you whether it's a valid date, but not whether it's in the format you want. For example, %@ISDATE[05-23-68] will return 1, even though the date isn't YYYYMMDD. Assuming that you already know the length is 8 characters, you could check for both a valid date and the desired format in one swell foop with something like:

Code:
if "%@isdate[%cdate]%@isdigit[%cdate]" == "11" echo Valid date in YYYYMMDD format.
(But note that my idea of a "valid date" includes dates well out of TCC's supported date range.)
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
3,602
46
Albuquerque, NM
prospero.unm.edu
#8
From the included HTML: %@ISDATE[date,infmt] so couldn't I use %@isdate[%cDate,YYYYMMDD] ??
That second parameter is only used to interpret ambiguous date formats, like 05-11-12. It doesn't enforce any format.

(Klaus's solution is also quite clever; have you checked it out? TCC will happily accept some invalid dates, the 30th of February or whatever. But pushing such an invalid date through @DATE and then @MAKEDATE will give you a valid date back -- which will necessarily be a different string from the one you started with. Elegant!)