Applescript string matching failed

applescript

I want it so that if the current date matches the string, it will log "works!". However it doesn't seem to work.

set datesubmit to "27, May, 2016"
set trydate to {day, month, year} of (current date)
log (trydate)
log (datesubmit)
if trydate is equal to datesubmit then
    log ("works!")
end if

I tried to make sure they are both string type variables but I can't get it to equal. This was the output:

(*27, May, 2016*)
(*27, May, 2016*)

Can someone tell me where I'm wrong?

Best Answer

After some trying I come to the conclusion that a variable set to {day, month, year} of (current date) isn't a "string" with the form "day, month, year" but "daymonthyear".

So you may change the script to

set datesubmit to "27May2016"
set trydate to {day, month, year} of (current date)
log (datesubmit)
log (trydate)
if trydate as string is equal to datesubmit then
    log ("works!")
else
    log ("doesn't work!")
end if

which is not very elegant.

Alternatively you may choose:

set datesubmit to "Friday 27 May 2016"
set trydate to date string of (current date)
log (datesubmit)
log (trydate)
if trydate is equal to datesubmit then
    log ("works!")
else
    log ("doesn't work!")
end if

You always have to add the weekday in the first variable though.

The best proposal (made by the OP itself) is:

set datesubmit to "27, May, 2016"
set trydate to day of (current date) & ", " & month of (current date) & ", " & year of (current date) as string
log (datesubmit)
log (trydate)
if trydate is equal to datesubmit then
    log ("works!")
else
    log ("doesn't work!")
end if