I am always working with dates and times, and, have very specific requirements as to how they are formatted. Specifically, the abbreviations for months and days and the representation of AM/PM, the use of noon and midnight.
Fixing this latest hiccup I found I have more than a dozen different handlers for getting date and time texts out of appleScript dates used in various scripts.
So, I’m took this opportunity to come up with a single handler to replace all of those. At some point I’ll put it a library and will give the lib a dictionary. But for now this will work nicely.
Thanks, Shane!
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
set aDate to current date
set time of aDate to 0 * hours
set aDate to aDate + 12 * days
set timeDateResults to {}
repeat 52 * 7 times
set thisDateTimeRecord to my FormatDateTime(aDate)
set the end of timeDateResults to frmtAbbDayDate of thisDateTimeRecord & " " & formattedTime of thisDateTimeRecord
set aDate to aDate + 30 * minutes
set aDate to aDate + 12 * days
end repeat
return timeDateResults
on FormatDateTime(someDate)
set wkdStrings to {"Sun.", "Mon.", "Tues.", "Wed.", "Thur.", "Fri.", "Sat."}
set mnthStrings to {"Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."}
set dateTimeRecord to {frmtLongDayDate:"", frmtAbbDayDate:"", frmtShrtDt:"", frmtShortDate:"", slugDate:"", slugDateSort:"", frmtTime:"", formattedTime:"", w:"", ww:"", WWWW:"", m:"", mm:"", mmm:"", MMMM:"", DD:"", d:"", yyyy:"", yy:"", hh:"", h:"", min:"", mi:"", ampm:""}
set formatter to current application's NSDateFormatter's new()
set aPattern to "ee EEEE MM MMMM dd yy yyyy hh mm aaaaa"
(formatter's setDateFormat:aPattern)
set formattedDateTime to (formatter's stringFromDate:someDate) as text
set {weekday2d, weekdayString, month2d, monthString, day2D, year2d, year4d, hour2d, minute2d, ampm} to words of formattedDateTime
tell dateTimeRecord
set its w to weekday2d as integer
set its ww to item (weekday2d as integer) of wkdStrings
set its WWWW to weekdayString
set its m to month2d as integer
set its mm to month2d
set its mmm to item (month2d as integer) of mnthStrings
set its MMMM to monthString
set its d to day2D as integer
set its DD to day2D
set its yy to year2d
set its yyyy to year4d
set its h to hour2d as integer
set its hh to hour2d
set its mi to minute2d as integer
set its min to minute2d
set its ampm to ampm & ".m."
set its frmtLongDayDate to (its WWWW & ", " & its MMMM & " " & (its d) as text) & ", " & its yyyy as text
set its frmtAbbDayDate to (its ww & ", " & its mmm & " " & (its d) as text) & ", " & its yyyy as text
set its frmtShrtDt to (its m as text) & "/" & (its d as text) & "/" & (its yy as text)
set its frmtShortDate to its mm & "/" & its DD & "/" & its yyyy as text
set its slugDateSort to (its yy as text) & "-" & its mm & "-" & its DD
set its slugDate to its DD & "-" & its mm & "-" & (its yy as text)
if its min is "00" then
set its frmtTime to (its h as text) & " " & its ampm
else
set its frmtTime to (its h as text) & ":" & its min & " " & its ampm
end if
set midNoonTime to its frmtTime
if midNoonTime is "12 a.m." then
set midNoonTime to "midnight"
else if midNoonTime is "12 p.m." then
set midNoonTime to "noon"
end if
set its formattedTime to midNoonTime
end tell
return dateTimeRecord
end FormatDateTime