Formatting the date in unix to include suffix on day (st, nd, rd and th)

后端 未结 3 1077
半阙折子戏
半阙折子戏 2021-02-15 00:26

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

3条回答
  •  你的背包
    2021-02-15 01:19

    #!/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"
    

提交回复
热议问题