Simple date script works fine in ASE, not Xcode

I have the following code in ASE and Xcode:


set runAt to (date string of (current date) & space & "12:45:12 PM")
if (time string of date runAt) is less than date "Wednesday, June 8, 2011 1:00:00 PM" then
     return "time to do something"
end if

with the only difference being “log” instead of “return”.

In ASE, it returns “time to run”. But Xcode returns the error:

Why? I don’t understand the error, nor why it is happening.

Thanks as always.

Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

You want to run some code whenever it is earlier than some fixed time?

(time string of date runAt) is less than date "Wednesday, June 8, 2011 1:00:00 PM"

That does not work as you want it to work. It compares a string (containing a time, but a string nevertheless) with a date object. Result is always “false”, of course.
Just compare the two date objects.

A “literal” date object does not compile on systems with a different date format.
Create arbitrary date objects like so:

set fixedTime to current date
set time of fixedTime to 13 * hours -- 1PM
fixedTime -->date "donderdag 9 juni 2011 13:00:00"

The reply is in MY system’s date format, and it won’t work when you put it in your code snippet.

This will work regardless of the system’s date format:

set fixedTime to current date
set time of fixedTime to 13 * hours -- 1PM
if (current date) < fixedTime then return "time to do something"

And a oneliner that does the same thing:

if time of (current date) < 13 * hours then return "time to do something"

Sorry for the delay. Been a rough couple of days. Thank you for the response. You are definitely right, I should compare by seconds, not a literal string.

I was unaware I could not coerce a string, formatted properly, into a date/date string. As I had here:

set runAt to (date string of (current date) & space & "12:45:12 PM")

The error is actually not happening during the if statement, it’s actually occurring when setting the variable runAt, yet works fine within Applescript Editor. Just seems strange that there is a lack of consistency there somewhere for some reason.

The only work-around I’ve found is to break down the date, painfully delimiter-by-delimiter, due to my 50,000 variations of coercion failures :slight_smile:

Thank you for the help and explanation!

Yes you can:

set runAt to date (date string of (current date) & space & "12:45:12 PM")
--> date "zaterdag 11 juni 2011 12:45:12"

Not knowing anything about Xcode I have no idea why Xcode would choke on that variable declaration, as you say. It’s a string - the error is about not being able to get a time string from some object. I’d say the object is not a date, so I’d expect the error to come from

(time string of date runAt)

Apparently Xcode has trouble to do the coercion. Might the trailing “PM” have anything to do with it? It’s redundant anyway, using 24-hours time notation.