I need to massage data in an SQLite database, and the date values are stored in the typical UTF format of YYYY-MM-DD. When working with the old AS Studio, I used this handler to convert those values to DD-MMMMMMM-YYYY, which I prefer for visual reasons.
on DMYDateFormat(utf)
try
set date_object to (date ((utf's word 2) & "-" & (utf's word 3) & "-" & (utf's word 1)))
return ((date_object's day) & space & (date_object's month) & space & (date_object's year) as text)
on error
return utf
end try
end DMYDateFormat
my DMYDateFormat("2009-05-21")
--> "21 May 2009"
This handler does not work in the new ASObjC environment; it returns the error: Can’t get day of class NSObject.
Upon closer inspection, it appears to be a compiling issue. Neither of the terms day nor year compile correctly in the try block, and although month does indeed compile, it returns the error Can’t make month into type text.
It is easily worked around in this fashion, but I really liked my former version.:
property month_List : {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
on DMYDateFormat(utf)
try
set date_object to (date ((utf's word 2) & "-" & (utf's word 3) & "-" & (utf's word 1)))
return ((utf's word 3) & space & (item (utf's word 2 as number) of month_List) & space & (utf's word 1) as text)
on error
return utf
end try
end DMYDateFormat
If I have missed something here, I would love to know about it.
It looks to me that you can can’t make a date from a string like that in ASObjC, although the changes to dates in AppleScript might also be involved. At any rate, the workaround is to use current date, like this:
set date_object to (current date)
set year of date_object to (utf's word 1)
set day of date_object to (utf's word 2)
set month of date_object to (utf's word 3)
And that’s going to be more reliable in 10.6’s AS anyway.
That worked great, Shane, thanks a lot. Here is the new handler:
on DMYDateFormat(utf)
try
set date_object to (current date)
set date_object's year to (utf's word 1)
set date_object's month to (utf's word 2)
set date_object's day to (utf's word 3)
return ((date_object's day as text) & space & (date_object's month as text) & space & (date_object's year as text) as text)
on error errmsg
return utf
end try
end DMYDateFormat
my DMYDateFormat("2009-05-21")
--> "21 May 2009"
And, this syntax does not work:
set date_object to (current date)
set date_object's {year, month, day} to {(utf's word 1), (utf's word 2), (utf's word 3)}
Hi Guys,
I picked up on this thread, because I am using ObjCandAS to make a program, and my date formatting is coming up:
Monday, August 15, 2011 1:39:03 PM
I used Shane Stanley’s method of Date Coercion from ObjectC to AS, and then I basically coerce this to text to write it to a file so I have a list of dates. However, the format mentioned above does not work in excel as a date format and it basically just categorizes it alphabetically.
The script is this:
try
set theDir to "cd ~/Library/Application\\ Support && mkdir iCalReminder"
do shell script theDir
end try
--set theNSDate1 to current application's NSDate's |date|() --today's date
set theDate to current date
set theDateString to theDate as text
set newDate to current application's date theDateString as date
set finalDate to newDate as string
log finalDate
set writtenData to (path to application support from user domain as string) & "iCalReminder:" & "iCalReminders.txt"
try
tell current application
set fileRefMail to (open for access file writtenData with write permission)
end tell
set newText to ¬
theCalendar & tab & finalDate & tab & theASDate as string & tab & eName & return
write newText to fileRefMail starting at eof
close access fileRefMail
--on error
try
tell current application
close access file writtenData
end tell
end try
end try
What I am trying to do is make finalDate format to something that leaves off the day of the week. Does this make sense? Thank you for your help.