IsDate Function

Hello All: I have three number (day, month, year) and I want to know if they are valid date. There is a function like isDate that can I use? TX Corrado

Use the EVALUATE function. IF EVALUATE(dateVariable, format(day) + format(month) + format(year)) THEN // date valid ELSE // date invalid

Tx. But the correct function is: IF EVALUATE(myDate, format(day) + ‘/’ + format(month) +’/’ + format(year)) THEN //TRUE ELSE //FALSE Corrado

I try to avoid these methods as they depend on the date format on your computer, so different users can get different results. You can create your own function which uses calcdate, so you don’t have to program any leap year logic.

clembo, you do not need the ‘/’. This, as well as every other punctuation character, will make the date interpretation depend on the current locale, as Chris pointed out. However, what actually is wrong with my code is that you need a FORMAT(xxx,2,’<Filler Character,0>’) instead of the simple FORMAT(xxx). This will cause the string to look like 210304 instead of the invalid 2134.

Here’s a function for you: Day := 29; Month := 2; Year := 2000; IF (Month > 0) AND (Month < 13) THEN BEGIN IF (day > 0) AND (DATE2DMY(CALCDATE(STRSUBSTNO('+%1D-1D',day),DMY2DATE(1,Month,1)),2) = Month) THEN datedings := DMY2DATE(Day,Month,Year) ELSE ERROR('Date invalid'); END ELSE ERROR('Date invalid');

Cool – how about this… IF (Year IN [0..9999]) AND (Month IN [1..12]) AND (Day > 0) AND (Day <= DATE2DMY(CALCDATE('<+CM>',DMY2DATE(1,Month,Year),1))) THEN datedings := DMY2DATE(Day,Month,Year) ELSE ERROR('Date invalid'); Question: Is left-to-right evaluation of compound conditionals guaranteed, or is there some risk that the DMY2DATE in the 4th conditional clause will be evaluated before the first and second clause have determined that the Year and Month are ok?

I think you made correct asumption. It is left-to-right evaluation. But you can test by setting Month=13.

Oops – my suggestion doesn’t work if the Month or Year is invalid. Apparently, C/AL does NOT do ‘short-circut evaluation’ of compound conditionals, so the indented IF’s are required. Here’s a fix: IF NOT (Year IN [0..9999]) THEN ERROR('Year is invalid') ELSE IF NOT (Month IN [1..12]) THEN ERROR('Month is invalid') ELSE IF (Day < 1) THEN ERROR('Day is invalid') ELSE IF (Day > DATE2DMY(CALCDATE('<+CM>',DMY2DATE(1,Month,Year)),1)) THEN ERROR('Day is invalid') ELSE datedings := DMY2DATE(Day, Month, Year);

Thanks for improving my function :slight_smile: Month IN [1…12] ← i like that, didn’t know that its possible to use it that way