Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ccstone/e8e4e0c5c7b15f422747 to your computer and use it in GitHub Desktop.
Save ccstone/e8e4e0c5c7b15f422747 to your computer and use it in GitHub Desktop.
Use AppleScriptObjC (ASObjC) to Create a date-string using ICU Date-Time Format Syntax
-------------------------------------------------------------------------------------------
# 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:
-------------------------------------------------------------------------------------------
@JMichaelTX
Copy link

Great date format tool! Beats any other AppleScript date formatter that I have seen, and does NOT require a shell script!
Many, many thanks to Shane and Chris!

NOTE: Requires Yosemite (10.10.5)+

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment