Date and Time strings in Sonoma

OK, here’s a version that incorporates your suggestions. It may look like it could be further simplified but the idea here is to generate a single record that accounts for every use of date or time I have in various scripts.

This runs significantly faster than using the pure appleScript version I was using before.

Eventually I’ll save it as a script library and add a dictionary.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
use framework "Foundation"
use framework "AppKit"

property formatter : {}
set aDate to current date
set timeDateResults to {}
repeat 52 * 7 times
   set thisDateTimeRecord to my FormatDateTime(aDate)
   set the end of timeDateResults to longDayDate of thisDateTimeRecord & " " & tm of thisDateTimeRecord
   set minuteMult to random number from 1 to 60
   set dayMult to random number from 1 to 7
   set aDate to aDate + minuteMult * minutes
   set aDate to aDate + dayMult * days
end repeat

on DateTimeFormatter()
   set formatter to current application's NSDateFormatter's new()
   formatter's setShortWeekdaySymbols:{"Sun.", "Mon.", "Tues.", "Wed.", "Thurs.", "Fri.", "Sat."}
   formatter's setShortMonthSymbols:{"Jan.", "Feb.", "March", "April", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."}
   formatter's setAMSymbol:"a.m."
   formatter's setPMSymbol:"p.m."
end DateTimeFormatter
on FormatDateTime(someDate)
   set dateTimeRecord to {longDayDate:"", abbDayDate:"", shrtDt:"", shortDate:"", slugDate:"", slugDateSort:"", timeString:"", slugTime:"", wwww:"", ww:"", mmmm:"", mmm:"", mm:"", m:"", dd:"", d:"", yy:"", yyyy:"", tm:"", hh:"", h:"", min:"", mn:"", ampm:""}
   set aPattern to "EEEE   EEE   MMMM   MMM   MM   dd   yy   yyyy   h:mm a   mm   HH   H   a"
   (formatter's setDateFormat:aPattern)
   set formattedDateTime to (formatter's stringFromDate:someDate)
   set formattedDateTime to formattedDateTime's stringByReplacingOccurrencesOfString:"   12:00 a.m." withString:"   midnight"
   set formattedDateTime to formattedDateTime's stringByReplacingOccurrencesOfString:"   12:00 p.m." withString:"   noon"
   set formattedDateTime to formattedDateTime's stringByReplacingOccurrencesOfString:":00" withString:""
   set saveTID to AppleScript's text item delimiters
   set AppleScript's text item delimiters to {tab}
   set {wwww, ww, mmmm, mmm, mm, dd, yy, yyyy, tm, min, hh, h, ampm} to text items of (formattedDateTime as text)
   set AppleScript's text item delimiters to saveTID
   tell dateTimeRecord
      set its wwww to wwww
      set its ww to ww
      set its mmmm to mmmm
      set its mmm to mmm
      set its mm to mm
      set its m to mm as integer
      set its dd to dd
      set its d to dd as integer
      set its yy to yy
      set its yyyy to yyyy
      set its tm to tm
      set its hh to hh
      set its h to h as integer
      set its min to min
      set its mn to min as integer
      set its ampm to ampm
      set its longDayDate to (wwww & ", " & mmmm & " " & (its d) as text) & ", " & yyyy
      set its abbDayDate to (ww & ", " & mmm & " " & (its d) as text) & ", " & yyyy
      set its shrtDt to (its m as text) & "/" & (its d as text) & "/" & yy
      set its shortDate to mm & "/" & dd & "/" & yyyy
      set its slugDateSort to yy & "-" & mm & "-" & dd
      set its slugDate to dd & "-" & mm & "-" & yy
      set its slugTime to hh & "-" & mm
   end tell
   return dateTimeRecord
end FormatDateTime

1 Like