Back to list

Formatting dates with ordinal numbers

Typinator can insert the current date in expansions. This feature is very flexible because you can assemble the date in many different formats by combining various elements for month, day, and year. All the elements can be accessed by clicking the {…} menu in Typinator's mini menu bar.

For example, {NN} {D} produces the date without the year in the form month+day, like "June 2" or "June 21". One element seems to be missing: If you want to write the date with an ordinal number in the form "June 2nd" or "June 21st", how can you do that?

Fortunately, Typinator comes with a set of built-in functions that can transform text fragments in a variety of ways. To create ordinal numbers for days, you can do this with the built-in Case function. When you select the Case function from the "Built-in Functions" in the {…} menu, a template is inserted in your expansion, like this:

{/Case /value/case1/result1/case2/result2/…/…/default result/}

The "value" is used to decide which result is produced. After that, an arbitrary number of case/result pairs define the results for individual cases. At the end, a single extra argument can be added as the fallback value that is to be used for all cases that are not listed explicitly.

For our task, we use the day number {D} as the input value, and then we describe the special cases for "st", "nd" and "rd":

{/Case /{D}/1/st/2/nd/3/rd/21/st/22/nd/23/rd/31/st/th/}

The cases are processed from left to right until a match is found. If the day number is 1, the result is "st". If the day number is 2, the result is "nd", etc. At the end, the fallback value "th" is specified for all days that are not listed as special cases.

Now just append the Case function immediately after the day element:

{NN} {D}{/Case /{D}/1/st/2/nd/3/rd/21/st/22/nd/23/rd/31/st/th/}

That's it. On the first days of June, the results are:

June 1st
June 2nd
June 3rd
June 4th
and so on…

Of course, you can also add the year to the date by appending a comma and the year marker:

{NN} {D}{/Case /{D}/1/st/2/nd/3/rd/21/st/22/nd/23/rd/31/st/th/}, {YYYY}