1. checks for us and euro date formats 2. check fo...
# cfml-general
g
3. checks for us and euro date formats 4. check for the dtaes as start date cannot have more than last 2 years and end date cannot be in more than next 2 years https://trycf.com/gist/521139c666055d284b4a1f4bd7a25964/acf2023?theme=monokai but there seems some issues with my code, can anyone provide some guidance to fix the issues i am having, its not checking for valida dates
t
The error seems pretty self-explanatory...
parseEuroDate
doesn't check that
euroDate
is a valid date before is calls
LSParseDateTime
so that throws an error on the invalid values.
I think I would do this with try/catch rather than if/else.
Copy code
try {
        var startDate = parseEuroDate(startEuroDate);
        var endDate = parseEuroDate(endEuroDate);
    }
    catch( any error ) {
        return "Error: Invalid date format.";
    }
g
do you only issue one issue, are my test cases for the things i am cehcking
so instead of using parseEurodate, i should updae it according to lsparsedate
t
Your error is that you don't check if
startEuroDate
and
endEuroDate
are dates until after you have attempted to convert them to date types.
isUsefulDate
checks if startDate and endDate are dates... But they have to be dates, because
parseEuroDate
returns a date.
g
i am see if isDate can return me true / false if the date is eurodate or US Date
t
that
IsDate
check needs to move into
parseEuroDate
instead. But even then, it wouldn't actually solve the problem because "12/23/2023" would pass an
IsDate
check. But still cause
LSParseDateTime
to throw an error, since it's not a valid UK date.
So just taking out the
IsDate
check, and checking the exception is probably the easiest way to handle this.
g
can you edit my fiddle so i know what exactly you are referring to
would be easier to understand this way
actually, looking more closely at your code, I've got another tweak. https://trycf.com/gist/3ae1497cbd345ef82b12a97d2a53e417/lucee5?theme=monokai
g
i think in the catch, i can do this: try { var startDate = parseEuroDate(startEuroDate); var endDate = parseEuroDate(endEuroDate); } catch( any error ) { var startDate = startEuroDate; var endDate = endEuroDate; }\
because if it fails in that trym,, it means the date passed is a different format
t
it could also be invalid in any format. If you try doing that to your code, with your test inputs, you just move your error to
var startYear = year(startDate)
because "45/45/2023" is not a date, no matter what format you're using.
This "fixes" it, but still ends up with weird behavior, if you look at my comments https://trycf.com/gist/d7bdc3c6ba8df942506c47e5a5a067a9/lucee5?theme=monokai
it's really safest to assume it's garbage if it's not in the Euro date format, and just stop attempting to process it.
👍 1
g
then in this case, can't we use datecompare to do the differences or convert them to unix timestamp for comparisions if(startDate > endDate){ return "Error: Start date is greater than end date."; } if(startYear < currentYear - 2 || startYear > currentYear + 2){ return "Error: Start date year is not within the last two years or the next two years."; } if(endYear < currentYear - 2 || endYear > currentYear + 2){ return "Error: End date year is not within the last two years or the next two years."; }