Ordinal dates?

Hello,

I’ve googled around with no luck to find a way to use AS to generate ordinal day dates, i.e. 1st, 2nd, 3rd, 4th, up to 31st. I’d like to be able to enable a script with a keystroke for easy pasting of the current ordinal day into several ongoing documents.

I found a way to do generate ordinals via a php shell script, but does anyone have a native applescript method?

Thanks, Mark

This handler by one of our moderators, Nigel Garvey, does it nicely with surprising brevity:

-- Append an English ordinal suffix to a number, returning the result as string.
on ordinalise(n)
	set units to n mod 10
	if (units > 3) or ((n - units) mod 100 is 10) or (units < 1) or (units mod 1 > 0) then return (n as string) & "th"
	return (n as string) & item units of {"st", "nd", "rd"}
end ordinalise

ordinalise(22) --> "22nd"

If you prefer the American spelling, change ordinalise to ordinalize.

It was in this Code Exchange thread

Thanks, really appreciate it; that is a slick method, and no php involved.

:slight_smile: And since I live in Montana, I can change ordinalise to:

on giddyup_and_ ride_herd_on_them_dates_and_ordinalize_them(n) 

Visions of it now appearing all over the Internet with that name! :/:wink: It would just beat a handler in one of my ScriptBuilders efforts:

on DateOfThisInstanceOfThisWeekdayBeforeOrAfterThisDate(d, w, i)

As it’s two years and a few days since kai suddenly fell off the radar, I’d like to dedicate this specially-composed version of ordinalise(n) to him: :slight_smile:

-- Append an English ordinal suffix to a number, returning the result as string.
on ordinalise(n) (* Special "kai" edition. *)
	tell n mod 10 to (n as string) & item (it * (((it < 4) and (it > 0) and ((n - it) mod 100 is not 10) and (it mod 1 is 0)) as integer) + 1) of {"th", "st", "nd", "rd"}
end ordinalise

ordinalise(22) --> "22nd"

For all those of us who knew and remember Kai’s excruciatingly complex one-liners, your ordinalize revision is indeed a tribute, Mr. G. :smiley:

The use of his expression “Mr. G” being your own tribute, of course. :slight_smile: