Coercing Dates

I have a strange issue , when trying to coerce dates. The following script logs result of checking if 12/21/21 is greater than January 1,2022 or February 1, 2022 in the 1st case it returns true which is obviously incorrect in the second false which is correct. I checked every month and it returns the same error for Oct, Nov & Dec. So it appears as if a “1” in the month is the problem.
Does anyone have any suggestions or can tell me what I may be doing incorrectly.
this is the log of the results

(Ex 12/21/21 Opt 1/1/22 Ex > Opt true)
(Ex 12/21/21 Opt 2/1/22 Ex > Opt false)

set ExdivDate to "12/21/21"
set ExdivDate to date ExdivDate
set Optdate to "January 1,2022"
set Optdate to date Optdate
set ExdivDate to short date string of ExdivDate
set Optdate to short date string of Optdate
log "Ex " & ExdivDate & " Opt " & Optdate & " Ex > Opt " & (ExdivDate > Optdate)
set ExdivDate to "12/21/21"
set ExdivDate to date ExdivDate
set Optdate to "February 1,2022"
set Optdate to date Optdate
set ExdivDate to short date string of ExdivDate
set Optdate to short date string of Optdate
log "Ex " & ExdivDate & " Opt " & Optdate & " Ex > Opt " & (ExdivDate > Optdate)

You should show the output of your log

You may want to look into using NSDate
And use NSDateFormatter and NSDate compare methods

https://developer.apple.com/documentation/foundation/nsdate?language=objc

As soon as you get the short date string of your dates, you are comparing strings, not dates. As such, the result is correct. Remember, strings are compared character by character. You need to compare the actual dates, not their string representations.

Given this input:


set test1 to "12/21/21"
set test2 to "1/1/22"
log test1 > test2
(*true*)

Because:
“1” == “1”
“12” > “1/”


set date1 to date "12/21/21"
set date2 to date "1/1/22"
log date1 > date2
(*false*)

Because:
December 2021 < January 2022


set test3 to "12/21/21"
set test4 to "2/1/22"
log test3 > test4
(*false*)

Because:
“1” < “2”