2

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?

8
  • 2
    In your example datesubmit and trydate are not both strings and therefore your if statement is doing exactly what it's supposed to because they are not equal. You cannot go by the appearance of what the log command shows! You should probably read Class Reference, specifically the sections on date and text. Change set trydate to {day, month, year} of (current date) to set trydate to date string of (current date) and it should then compare as strings. May 27, 2016 at 4:16
  • 1
    I don't think trydate is set to a string as you expect because if you change datesubmit to "27May2016" it works!
    – klanomath
    May 27, 2016 at 4:21
  • Klanomath post it as an answer so I can mark you as correct.
    – Bradman175
    May 27, 2016 at 4:38
  • @klanomath, you said "... if you change datesubmit to "27May2016" it works!", well it doesn't work on my system when I do that! May 27, 2016 at 4:41
  • @user3439894 well it works for my system. Change log(trydate) to log(trydate as string). Tell use what happens. Klanomath solved my problem.
    – Bradman175
    May 27, 2016 at 4:48

2 Answers 2

2

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
6
  • I just did: set trydate to day of (current date) & ", " & month of (current date) & ", " & year of (current date) as string
    – Bradman175
    May 27, 2016 at 5:14
  • Then you can omit the "as string" in the if statement.
    – Bradman175
    May 27, 2016 at 5:15
  • @Bradman175 I tried similar solutions but AppleScript-Editor always complained about them because I got all the &"() etc. wrong...
    – klanomath
    May 27, 2016 at 5:19
  • does mine not work though?
    – Bradman175
    May 27, 2016 at 5:21
  • @Bradman175 It works of course ;-)
    – klanomath
    May 27, 2016 at 5:22
2

klanomath's helpful answer contains viable solutions, but the cleanest and simplest approach is to compare items of the same type directly:

set datesubmit to {27, May, 2016}
set trydate to {day, month, year} of (current date)

if trydate = datesubmit then
    log "works!"
end if

Both date variables now contain 3-element lists ({ ..., ..., ... }) that can be directly compared with is equal to or simply =.

Note that May is a built-in month constant that evaluates to 5 in a numerical context (it is not a string).

4
  • 1
    Ooh trying to steal klanomath's place lol. Yours does look better tho.
    – Bradman175
    May 27, 2016 at 12:53
  • if trydate = datesubmit then was one of the first things I tried and it didn't work on my system, not sure why though. Although, today it's working! +1 May 27, 2016 at 14:23
  • 1
    I just figured out why it didn't work... at the time I tried it was still the 26th as it was about quarter till midnight here at my location. So it actually worked by not outputting (*works!*) as it wasn't the 27th yet. May 27, 2016 at 14:29
  • Why can't I mark 2 as correct?
    – Bradman175
    May 28, 2016 at 1:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .