Believe it or not, I've googled all over for this simple thing and can't find it anywhere. I have a string which has the date in "YYYY-MM-DD" format, but I generated it from a shell script, so it can be anything. Applescript however seems to only accept a very specific string format which I can't guess and can't find documentation for. I've tried every format I can think of. Shockingly I can't even get this code to work, which takes Applescript's own format and just feeds it back:
set theDate to (current date) as string
set tDate to theDate as date
Whereupon applescript issues the error "Can’t make "Saturday, April 1, 2023 at 14:49:59" into type date."
How is this even possible? Can anyone please tell me the magic format?
I know it might depend on my system settings, but so how do I find what my settings are right now and give the script the right string?
Thanks!
If I look in the Applescript dictionary, it provides a non-descriptive answer. Just says "In constructing a date, you may use any string value that can be interpreted as a date, a time, or a date-time." Examples please?
Related topics has already been chewed and chewed many times. I'll just repeat what should work specifically for the case "YYYY-MM-DD".
You have the wrong question here. The point is that you need to put the order of "YYYY-MM-DD" in accordance with the order in your locale. In different locales, the order of Year, Month, Day is different and will often not be "YYYY-MM-DD".
AppleScript can't guess the right order just by coercion, automatically. This does not work. Moreover, this was not to be expected. How, for example, could AppleScript discern what is a day and what is a month in a pair ("11", "05")? No way.
Therefore, you have to specify the order explicitly by writing the correct transform (better as handler).
myShellStringDateToAppleScriptDate("2024-11-05") -- string's order is not localized: YYYY-MM-DD
on myShellStringDateToAppleScriptDate(shellDate)
set AppleScriptDate to (current date) -- creates new date object with localized order
tell AppleScriptDate -- put the order of "YYYY-MM-DD" in accordance with the order in your locale
set year to text 1 thru 4 of shellDate
set its month to text 6 thru 7 of shellDate
set day to text 9 thru 10 of shellDate
end tell
return AppleScriptDate
end myShellStringDateToAppleScriptDate
Note:
Shells provide a way for passing a date with an indication of the localization order. There is a so-called ISOT string for this. Correct transmission of the date between remote users via shells requires exactly the ISOT format. Given an ISOT string, you can convert it to an AppleScript date using AsObjC code:
use AppleScript version "2.5"
use framework "Foundation"
use scripting additions
set isotDate to "2020-05-07T06:05:34Z" -- ISOT string returned from shell
set formatter to current application's NSISO8601DateFormatter's new()
set theAppleScriptDate to (formatter's dateFromString:isotDate) as date
set theAppleScriptDate to theAppleScriptDate - (time to GMT)
Finally, AsObjC can also be used by explicitly specifying the shell string format:
use framework "Foundation"
use scripting additions
set todaysDate to do shell script "date '+%Y-%m-%d'" --> "2023-04-02"
set df to current application's NSDateFormatter's new()
df's setDateFormat:"y-M-d" -- related "YYYY-MM-DD" format
set theDate to (df's dateFromString:todaysDate) as date