All,
I have datepickers in my app and I need to format their value to “mm/dd/yyyy” format.
Currently I get the output in following form: Sunday, January 1, 2012 8:00:00 AM Pacific Standard Time
Here the code:
script AppDelegate
property aStartDate : missing value # Linked in IB
property aEndDate : missing value # Linked in IB
on submitForm()
set stdate to aStartDate's stringValue() as string
set enddate to aEndDate's stringValue() as string
display dialog stdate # Sunday, January 1, 2012 8:00:00 AM Pacific Standard Time
end submitForm
end script
set stdate to aStartDate's dateValue()
set theCal to current application's class "NSCalendar"'s currentCalendar()
set theComponents to theCal's components_fromDate_(254, stdate)
set theYear to theComponents's |year|() as string
set theMonth to theComponents's |month|() as string
set theDay to theComponents's |day|() as string
display dialog (theMonth & "/" & theDay & "/" & theYear)
set stdate to current application's NSDate's |date|()
set df to current application's NSDateFormatter's alloc()'s init()
df's setDateFormat_("mm/dd/yyyy")
return df's stringFromDate_(stdate)
set stdate to current application's NSDate's |date|()
return stdate's descriptionWithCalendarFormat_timeZone_locale_("%m/%d/%Y", missing value, missing value)
There are two problems with that in this case, though: first, the user wants the year as yyyy, not yy, and second, he’s starting off with an NSDate, not an AS date, so short date string isn’t applicable.
Yep, although the docs say: “You are strongly encouraged to migrate to using v10.4 behaviour” and “There are several problems with the implementation of this method that cannot be fixed for compatibility reasons. To format a date, you should use a date formatter object instead.”
set theNSDate to aStdate's dateValue()
set df to current application's NSDateFormatter's alloc()'s init()
df's setDateFormat_("mm/dd/yyyy")
set mystdate to df's stringFromDate_(theNSDate)
log mystdate