Skip to content

Instantly share code, notes, and snippets.

  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JMichaelTX/599a7099030f219b2e03 to your computer and use it in GitHub Desktop.
AppleScript (ASObjC) Format Date/Time Using ISO/ICU Format Codes

MODS BY JMichaelTX on Sat, Feb 27, 2016

AppleScript Handler: on formatDate(pDate, pFormat)

  1. Changed record-style parameters to single-field parameters
  2. Added block to handle date parameter as a string, including International format (YYYY-MM-DD)
  3. Added handler/function: convertIntlDate(pDateStr)
-------------------------------------------------------------------------------------------
# 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
-------------------------------------------------------------------------------------------
# MODS BY JMichaelTX on Sat, Feb 27, 2016
#
# 1. Changed record-style parameters to single-field parameters
# 2. Added block to handle date parameter as a string, including International format (YYYY-MM-DD)
# 3. Added handler/function: convertIntlDate(pDateStr)
-------------------------------------------------------------------------------------------
use framework "Foundation"
use scripting additions
log (my formatDate((current date), "ddMMMYY"))
log (my formatDate((current date), "dd MMMM YY"))
log (my formatDate((current date), "YYYY-dd-MM HH:mm:ss"))
log (my formatDate((current date), "YYYY-dd-MM hh:mm:ss a"))
log (my formatDate("Feb 10, 2016", "YYYY-dd-MM hh:mm:ss a")) ## VALID DATE, BUT AS STRING
--log (my formatDate("15 Jan 2016", "YYYY-dd-MM hh:mm:ss a")) ## INVALID DATE FORMAT AS STRING
-------------------------------------------------------------------------------------------
--» HANDLERS
-------------------------------------------------------------------------------------------
on formatDate(pDate, pFormat) ##CHG: JM REPLACED NAMED PARAMS
## For Format Codes, see
## http://userguide.icu-project.org/formatparse/datetime#TOC-Date-Time-Format-Syntax
##CHG: JM ADD BLOCK TO HANDLE pDate AS A STRING
if class of pDate is not date then
try -- Try Standard U.S. date format --
set pDate to date pDate
on error --- Try International Date format (YYYY-MM-DD)
set pDate to convertIntlDate(pDate)
end try
end if
##END CHG
set pDate to my makeNSDateFrom(pDate)
set theFormatter to current application's NSDateFormatter's new()
##CHG: CCS/SS NEW: fix for mutable 12h/24h time.
theFormatter's setLocale:(current application's NSLocale's localeWithLocaleIdentifier:"en_US_POSIX")
theFormatter's setDateFormat:pFormat
set theString to theFormatter's stringFromDate:pDate
return theString as text
end formatDate
-------------------------------------------------------------------------------------------
on makeNSDateFrom(pDate)
set {theYear, theMonth, theDay, theSeconds} to pDate'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
-------------------------------------------------------------------------------------------
to convertIntlDate(pDateStr)
--- AUTHOR: JMichaelTX
--- VER: 2.0 2016-02-24
--- pDateStr MUST be in the format of YYYY<delim>MM<delim>DD
--- where <delim> can be any character
--- like 2016-01-05
set resultDate to the current date
--––––––––––
try
--––––––––––
set the year of resultDate to (text 1 thru 4 of pDateStr)
set the month of resultDate to (text 6 thru 7 of pDateStr)
set the day of resultDate to (text 9 thru 10 of pDateStr)
set the time of resultDate to 0
if (length of pDateStr) > 10 then
set the hours of resultDate to (text 12 thru 13 of pDateStr)
set the minutes of resultDate to (text 15 thru 16 of pDateStr)
if (length of pDateStr) > 16 then
set the seconds of resultDate to (text 18 thru 19 of pDateStr)
end if
end if
--––––––––––
on error
--––––––––––
set isValid to false
set CR to return
--- REPEAT UNTIL USER ENTERS A VALID DATE, OR CANCELS ---
repeat while not isValid
set msgStr to " *** ERROR ***" & CR ¬
& "Cannot make a valid AppleScript Date from:" & CR & pDateStr & CR & CR ¬
& "Please enter a valid date in the format of" & CR & "MM/DD/YYYY"
set titleStr to (name of me)
set answerStr to ""
set oAns to display dialog msgStr default answer answerStr ¬
with title titleStr with icon caution cancel button 1
set answerStr to text returned of oAns
--–––––––
try
--–––––––
set resultDate to date answerStr
set isValid to true
on error
set pDateStr to answerStr
--–––––––
end try
--–––––––
end repeat
--––––––––––
end try
--––––––––––
return resultDate
end convertIntlDate
@bachmay
Copy link

bachmay commented Jul 17, 2021

Interested 🙂

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