• 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 19:09:29 +0100


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

Hi list,
The reason is kind of long, but suffice it to say that when I need a date in a script, it has to be in a dictionary format so I can pull out specific pieces. To do this, I have a handler that takes a date object and hands me my dictionary. Using NSDates is causing problems, though. NSDateFormatter isn't really an option, again because of the way this script works and what it does, and it seems a waste to set up NSDateComponents (not to mention I can't figure out how to OR together the masks to get the information I want).

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.

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"

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




 _______________________________________________
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: Alex Hall <email@hidden>
References: 
 >Can't Coerce String to Date? (From: Alex Hall <email@hidden>)

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