• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Can't Coerce String to Date?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can't Coerce String to Date?


  • Subject: Re: Can't Coerce String to Date?
  • From: Yvan KOENIG <email@hidden>
  • Date: Fri, 13 Mar 2015 21:11:54 +0100


Le 13/03/2015 à 20:14, Alex Hall <email@hidden> a écrit :


On Mar 13, 2015, at 2:09 PM, Yvan KOENIG <email@hidden> wrote:


Le 13/03/2015 à 16:37, Alex Hall <email@hidden> a écrit :

Now that my newest script uses NSDates, I'm trying to give those to this handler. Of course it won't work directly, so I need to put the NSDate into the AppleScript date type. I thought it would be easy:

set startDateString to (e's startDate()'s |description|()) as text
set startDate to (word 2 of startDateString & "/" & word 3 of startDateString & "/" & word 1 of startDateString) as date

In that snippet, "e" is an EKEvent. I don't get an error requesting e's date; rather I get an error that "can't convert "03/20/2015" to date". Yet, according to Google, AS can easily convert a string like that. The time will be midnight, but I can add the time in once this part is working. What am I doing wrong, or did something change because I'm using Yosemite and/or scripting additions?


set theDate to date ( dateString )
works only if dateString obey to the format defined in the Time & Date preference pane.

Thanks for your response.
From what I read, AppleScript will accept any string, like "06/11/2000" or "1 1 13" and figure it out. Interpretation comes from the user's settings (that is, if the day or month is the first number) but the date object *should* result either way. Is this no longer the case?

The (quite) clean way is to start from a date and set its components one by one.


# I assume that you have grabbed these three components
set theYear to "2015"
set theMonth to "3"
set theDay to "20"
set theDateString to my buildShortDateString(theYear, theMonth, theDay)
--> "20/03/2015"

I'm working off the description property of an NSDate, which is itself a string. Actually, I thought initially that I could simply give the entire string to an AS date object, but no such luck, for some reason. Setting properties is something I can do, using words from the string, but it seems unnecessary.

on buildShortDateString(the_Year, the_Month, the_Day)
set aDate to date ("12/12/12")
set day of aDate to the_Day as integer
set month of aDate to the_Month as integer
set year of aDate to the_Year as integer
log aDate
(*date vendredi 20 mars 2015 00:00:00*)
return short date string of aDate
end buildShortDateString


Yvan KOENIG (VALLAURIS, France) vendredi 13 mars 2015 19:08:36



--
Have a great day,
Alex Hall


# I repeat, the string to convert must match the system settings.
try
set aString to "31 12 1943"
date aString
log result (*date "vendredi 31 décembre 1943 00:00:00"*)
on error
log "The string doesn't match the current format : " & date string of (current date)
end try
try
set aString to "31/12-43"
date aString
log result (*date "jeudi 31 décembre 2043 00:00:00"*)
on error
log "The string doesn't match the current format : " & date string of (current date)
end try
try
set aString to "12/31/1943"
date aString
log result
on error
log "The string doesn't match the current format : " & date string of (current date)
end try
try
set aString to "12/31-43"
date aString
log result
on error
log "The string doesn't match the current format : " & date string of (current date)
end try
log "Now surprising behavior. If somebody may explain it, I'm interested."
try
set aString to "12 31/1943"
date aString
log "???? " & result (*???? vendredi 13 mars 2015 12:31:00*)
on error
log "The string doesn't match the current format : " & date string of (current date)
end try
try
set aString to "12 31 1943"
date aString
log "???? " & result (*???? vendredi 13 mars 2015 12:31:00*)
on error
log "The string doesn't match the current format : " & date string of (current date)
end try

On the system used in French, lhe events log is :

(*date vendredi 31 décembre 1943 00:00:00*)
(*date jeudi 31 décembre 2043 00:00:00*)
tell current application
current date
(*The string doesn't match the current format : vendredi 13 mars 2015*)
current date
(*The string doesn't match the current format : vendredi 13 mars 2015*)
(*Now surprising behavior. If somebody may explain it, I'm interested.*)
(*???? vendredi 13 mars 2015 12:31:00*)
(*???? vendredi 13 mars 2015 12:31:00*)
end tell

It will be different on system running in English.

Yvan KOENIG (VALLAURIS, France) vendredi 13 mars 2015 21:11:24






 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Can't Coerce String to Date?
      • From: Thomas Fischer <email@hidden>
References: 
 >Can't Coerce String to Date? (From: Alex Hall <email@hidden>)
 >Re: Can't Coerce String to Date? (From: Yvan KOENIG <email@hidden>)
 >Re: Can't Coerce String to Date? (From: Alex Hall <email@hidden>)

  • Prev by Date: Re: Can't Coerce String to Date?
  • Next by Date: Re: What Does Error Return ?
  • Previous by thread: Re: Can't Coerce String to Date?
  • Next by thread: Re: Can't Coerce String to Date?
  • Index(es):
    • Date
    • Thread