Date formatting in ASObjC

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.

Hi Craig,

what’s about an ObjC solution?

[code]NSDateFormatter *inputFormatter, *outputFormatter;
NSString *utf, *newString;

utf = @"2009-12-21";

inputFormatter = [[NSDateFormatter alloc] init];
outputFormatter = [[NSDateFormatter alloc] init];
[inputFormatter setDateFormat:@"yyyy-MM-dd"];
[outputFormatter setDateFormat:@"d MMMM yyyy"];
newString = [outputFormatter stringFromDate:[inputFormatter dateFromString:utf]];
[inputFormatter release];
[outputFormatter release];
NSLog(@"%@", newString); // --> 21 December 2009 with locale en_US[/code]

Yeah, I had considered that, but I am trying to explore this new ASObjC beast and see what it can and cannot do.

If you “log class of date_object” it is NSObject
If you “log date_object’s description” it is NSObject

I’m not sure how to use “date” in AppleScriptObjC but this is clearly not
producing a date object.

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.

Have you tried declaring an actual NSDate?

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.

Boyd

Hi,

is this sufficient ?


set finalDate to newDate's short date string

Hi Stefan,
Thank you, I will give this a shot.

BOyd

Hi Stefan,
That certainly worked. Just out of curiosity, can I add the time to this (is there terminology for this). I am searching for this online.

Thank you,

Boyd

it’s pretty simple AppleScript code

set finalDate to newDate's short date string & space & newDate's time string

Oh yeah, I was thinking in a different way. Thank you

Boyd