This date thingy?

Posted on
Mon Oct 20, 2003 9:12 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

This date thingy?

I have a variable in Indigo that I am trying to convert to a date. Here is the code:

Code: Select all
set last_rain to date (value of variable "WMlastrain")


The script editor will pop back with a dialog saying : Indigo got an error: Can't get date "Monday, Oct 20, 2003 4:04:06 PM".

Even if I
Code: Select all
set somevar to value of variable "WMlastrain" as string
then try to create a date from that I get the same error. What am I doing wrong?

Posted on
Tue Oct 21, 2003 1:37 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

Re: This date thingy?

gregjsmith wrote:
I have a variable in Indigo that I am trying to convert to a date. Here is the code:

Code: Select all
set last_rain to date (value of variable "WMlastrain")


The script editor will pop back with a dialog saying : Indigo got an error: Can't get date "Monday, Oct 20, 2003 4:04:06 PM".

Even if I
Code: Select all
set somevar to value of variable "WMlastrain" as string
then try to create a date from that I get the same error. What am I doing wrong?

The problem is there is no built-in AppleScript function to coerce a string into a date. When compiling scripts hard coded time/date strings are automatically converted to date objects (you'll notice the format of the string changes based on your system time/date format prefs), but during runtime there isn't an automatic way to say, "convert this string (or variable holding a string) into a date."

I seem to recall seeing a AppleScript function that someone wrote to help handle this. I googled for it but didn't see it anywhere. Anyone have any code snippets to help with this?

regards,
matt

Posted on
Tue Oct 21, 2003 5:39 am
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

But if I
Code: Select all
set last_rain to date "Monday, Oct 20, 2003 4:04:06 PM"
then it works. Isn't "Monday, Oct 20, 2003 4:04:06 PM" a string?

Posted on
Tue Oct 21, 2003 8:19 am
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

(No subject)

gregjsmith wrote:
But if I
Code: Select all
set last_rain to date "Monday, Oct 20, 2003 4:04:06 PM"
then it works. Isn't "Monday, Oct 20, 2003 4:04:06 PM" a string?

In this case, no. The AppleScript editor compiles that into a date object when you save the script. Try changing the "Monday" to "Mon", or the "October" to "10". Then, re-compile the script -- note it always reverts back to the original format. If it were a true literal string it wouldn't change.

regards,
matt

Posted on
Tue Oct 21, 2003 12:23 pm
gregjsmith offline
Posts: 946
Joined: Apr 01, 2003
Location: Rio Rancho, NM

(No subject)

There was a suggestion over at Macscripter BBS to put it in a subroutine like this:

Code: Select all
on SubDate(TheString)
   return (date TheString) as date
end SubDate


I'll give it a try when I get home.

Posted on
Tue Oct 21, 2003 12:57 pm
matt (support) offline
Site Admin
User avatar
Posts: 21417
Joined: Jan 27, 2003
Location: Texas

(No subject)

gregjsmith wrote:
There was a suggestion over at Macscripter BBS to put it in a subroutine like this

Seems to work... There goes my theory. I wonder why it works in this case. :?:

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 22 guests