Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Best way to validate a string is a YYYYMMDD

Apr
1,822
17
What is the best way to validate the first 8 chars of a file name is a valid date of form YYYYMMDD please?

%@left[8,%fn] but what then?
 
In the "good old days" we did this without external plugins:

Parse the string to the form YYYY-MM-DD

echo %@makedate[%@date[%string,4],4]

shouldn't produce an error an be identical to the value of %string.
 
Does @isdate[cDate,YYYYMMDD] work or would I still need to use isdigit[cDate] also?
 
@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.)
 
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!)
 
Back
Top
[FOX] Ultimate Translator
Translate