"date" and "short date string" weirdness

I do something very similar to Nigel’s, but my date strings are very customized to meet very specific publishing standards.

Months are three letters abbreviations except: March, April, June, July August, Sept.

Weekdays are three letter abbreviations except: Tues., Weds., and Thur.

For short date strings I need to generate these formats 1/1/25, 12/31/25; 01/01/2025; 12/31/2025; 2025-01-01

(Time formats are also idiosyncratic)

There really isn’t a library or system that can automate all of that so I use my own library.

And, of course, I only store dates and time as date formats

Just to confirm the above, I tested with a shortcut.

In this test, the date string is formatted as set in Date format in System Settings, and the result is as expected.

In this test, the date string is not recognized, so my region’s default date setting is used. Once again, the result is as expected.

This test most closely mimics brandelune’s script in post 1, and the result is as expected.

1 Like

Just as an exercise, although it’s nothing to do with short date strings:

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"

-- Months and weekdays in the same string. Normally a date string only has one of each!
set monthsAndWeekdays to "January February March April May June July August September October November December
Sunday Monday Tuesday Wednesday Thursday Friday Saturday"

set monthsAndWeekdays to current application's class "NSMutableString"'s stringWithString:(monthsAndWeekdays)
-- Match groups of three or more alphabetic characters which either:
-- end with "day" — in which case capture characters 1 thru 3 ($1) and any other required in that weekday ($2) —
-- or don't — in which case capture characters 1 thru 3 ($1) and any others required in that month ($3).
set pattern to "\\b([A-Z][a-z]{2})(?:(?:ne)?([sr])?[a-z]{0,2}day|(t|[a-z]{0,3}+\\b)?[a-z]*+)"
tell monthsAndWeekdays to replaceOccurrencesOfString:(pattern) withString:("$1$2$3") options:(current application's NSRegularExpressionSearch) range:({0, its |length|()})
-- Put a full stop after any "s" at the end of a word.
tell monthsAndWeekdays to replaceOccurrencesOfString:("s\\b") withString:("s.") options:(current application's NSRegularExpressionSearch) range:({0, its |length|()})
return monthsAndWeekdays as text
(* -->
"Jan Feb March April May June July August Sept Oct Nov Dec
Sun Mon Tues. Weds. Thur Fri Sat" *)
1 Like