12

How can I add the suffix on the day number of a unix date?

I'll explain. I have a TextMate bundle snippit that writes out today's date. It uses unix date and formatting. Here is the code:

`date +%A` `date +%d` `date +%B` `date +%Y` 

It outputs:

Monday 22 March 2010

I would like to add the suffix to the day (st, nd, rd and th) like so:

Monday 22nd March 2010

As far as I can see, there is no native function in the unix date formatting, like there is in PHP (j). How would I achieve this in unix? A complicated regex on the day number?

2
  • Not actually answering your question, but I'd consolidate the date commands: date "+%A %d %B %Y"-> Monday 22 March 2010 Commented Mar 22, 2010 at 20:30
  • If I were you, I'd write a small PHP script (since you already know how to do it there), and execute it. I don't know, if it's possible to install PHP on the target system though. Commented Mar 22, 2010 at 20:48

3 Answers 3

17

Try.

#!/bin/sh DaySuffix() { case `date +%d` in 1|21|31) echo "st";; 2|22) echo "nd";; 3|23) echo "rd";; *) echo "th";; esac } date "+%A %d`DaySuffix` %B %Y" 
Sign up to request clarification or add additional context in comments.

3 Comments

date +%-d might match better to prevent buffering with leading 0's.
I had to change the script to include leading zeros, e.g., 01|21|31) echo "st";; (I'm on macOS).
@robenkleene, on macOS I used date +%e to avoid the zero-padding altogether.
6

I have something similar working on a Linux machine (Ubuntu 8.10). I don't think it will work with Solaris, the one machine I tested did not allow using a _ character following the % to avoid padding the field with a 0. The non-padding allows date to return 1 instead of 01 (01st doesn't look right versus 1st).

I use a shell function (again, your OS or shell version may not like the way I defined the function) named DaySuffix, then call that func as part of the date call. The func itself is fairly hacky, I'm sure there is a better way to do this but it works for me. Note the special cases for 11, 12, & 13 - you've got to love the English language!

#!/bin/sh DaySuffix() { if [ "x`date +%-d | cut -c2`x" = "xx" ] then DayNum=`date +%-d` else DayNum=`date +%-d | cut -c2` fi CheckSpecialCase=`date +%-d` case $DayNum in 0 ) echo "th" ;; 1 ) if [ "$CheckSpecialCase" = "11" ] then echo "th" else echo "st" fi ;; 2 ) if [ "$CheckSpecialCase" = "12" ] then echo "th" else echo "nd" fi ;; 3 ) if [ "$CheckSpecialCase" = "13" ] then echo "th" else echo "rd" fi ;; [4-9] ) echo "th" ;; * ) return 1 ;; esac } # Using consolidated date command from chris_l # Also using %-d instead of %d so it doesn't pad with 0's date "+%A %-d`DaySuffix` %B %Y" 

1 Comment

I was able to use this code to fix my problem. However, I could not use +%-d because the dash is not allowed. Only strict unix formatting. en.wikipedia.org/wiki/Date_(Unix). I used +%e in it's place and it works well. Although I think it adds an extra space with single digits (like 1-9). I'll fix that problem on the 1st April. Thanks for the help. ;)
1
#!/bin/ksh DateSuffix() { if [ "$1" -eq "1" ] || [ "$1" -eq "21" ] || [ "$1" -eq "31" ] then echo 'st' elif [ "$1" -eq "2" ] || [ "$1" -eq "22" ] then echo 'nd' elif [ "$1" -eq "3" ] [ "$1" -eq "23" ] then echo 'rd' else echo 'th' fi } date "+%A %d`DateSuffix` %B %Y" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.