Skip to content

Instantly share code, notes, and snippets.

@rawiriblundell
Created May 10, 2018 02:56
Show Gist options
  • Save rawiriblundell/cd3bd422ea9c5383de8523f1d5d8347d to your computer and use it in GitHub Desktop.
Save rawiriblundell/cd3bd422ea9c5383de8523f1d5d8347d to your computer and use it in GitHub Desktop.
Add ordinal date representation i.e. Mar 2 => Mar 2nd
date() {
case "$@" in
(*"%o"*)
declare -a args1
declare -a args2
while [[ -n "$1" ]]; do
args2+=("$1")
if [[ "${1:0:1}" != + ]]; then
args1+=("$1")
fi
shift
done
case $(command date +%d "${args1[@]}") in
(01|21|31) dSfx="st";;
(02|22) dSfx="nd";;
(03|23) dSfx="rd";;
(*) dSfx="th";;
esac
command date "${args2[@]}" | sed -e "s/%o/${dSfx}/g"
;;
(*)
command date "$@"
;;
esac
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment