------------------------------------------------------------------------------------------- # Auth: Shane Stanley & Christopher Stone # dCre: 2014/01/19 09:46 +1100 # dMod: 2016/02/24 15:18 -0600 # Appl: AppleScriptObjC # Task: Create a date-string using ICU Date-Time Format Syntax # : http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax # Libs: None # Osax: None # Tags: @Applescript, @Script, @AppleScriptObjC, @ASObjC, @Shane, @Date, @String ------------------------------------------------------------------------------------------- use framework "Foundation" use scripting additions ------------------------------------------------------------------------------------------- my formatDate:(current date) usingFormat:"dd MMM y" --> "24 Feb 2016" my formatDate:(current date) usingFormat:"dd MMMM yyyy" --> "24 February 2016" my formatDate:(current date) usingFormat:"y-dd-MM hh:mm:ss a" -- 12h time --> "2016-24-02 03:28:56 PM" my formatDate:(current date) usingFormat:"y-dd-MM HH:mm:ss" -- 24h time --> "2016-24-02 15:28:56" ------------------------------------------------------------------------------------------- --» HANDLERS ------------------------------------------------------------------------------------------- on formatDate:theDate usingFormat:formatString if class of theDate is date then set theDate to my makeNSDateFrom:theDate set theFormatter to current application's NSDateFormatter's new() theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX") theFormatter's setDateFormat:formatString set theString to theFormatter's stringFromDate:theDate return theString as text end formatDate:usingFormat: ------------------------------------------------------------------------------------------- on makeNSDateFrom:theASDate set {theYear, theMonth, theDay, theSeconds} to theASDate's {year, month, day, time} if theYear < 0 then set theYear to -theYear set theEra to 0 else set theEra to 1 end if set theCalendar to current application's NSCalendar's currentCalendar() set newDate to theCalendar's dateWithEra:theEra |year|:theYear |month|:(theMonth as integer) ¬ |day|:theDay hour:0 minute:0 |second|:theSeconds nanosecond:0 return newDate end makeNSDateFrom: -------------------------------------------------------------------------------------------