Dates: Works in Applescript but not in ASOC

I am trying to find an age by subtracting birth date from current date. This works in Applescript:

set alder1 to "JULY 13, 1975"
set alder2 to (((current date) - (date alder1)) / 60 / 60 / 24 / 365) as integer

but it doesn´t work in ASOC. The error message is:

Invalid date and time date JULY 13, 1975 of class NSObject. (error -30720)

I can´t change the original input date format.

I had to change the date format settings of MacOSX before it worked in Script Editor. Do I have to toggle some settings in Xcode to achieve the same in ASOC?


a few lines more than the AppleScript version, but this is the Cocoa way to calculate dates.
By the way it also returns the correct result of 39 (AppleScript: 40) with the current date between January 1, 2015 and July 12, 2015

       set alder1 to "JULY 13, 1975"
        set formatter to current application's NSDateFormatter's alloc()'s init()
        formatter's setDateFormat:"MMM dd, yyyy"
        set usLocale to current application's NSLocale's alloc()'s initWithLocaleIdentifier:"en_US"
        formatter's setLocale:usLocale
        set now to current application's NSDate's |date|()
        set ageComponents to current application's NSCalendar's currentCalendar's components:current application's NSYearCalendarUnit fromDate:(formatter's dateFromString:alder1) toDate:now options:0
        set alder2 to ageComponents's |year|() as integer

Edit: the date format assumes a leading zero for e.g. JULY 01, 1975, If there is only one digit, change the date format to “MMM d, yyyy”

More correctly, it doesn’t work in ASObjC in an Xcode project. There are problems with some specifiers, and with forming a date using the date keyword and a string. You have to use something cumbersome like:

set theDate to current application's date theDateString as date

If possible, you’re better off using the date components and building a date that way. Creating dates from strings relies completely on system preferences that any user can change.