Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save scfcode/09a87b49e4558b1d99f7eecec1aacfa2 to your computer and use it in GitHub Desktop.

Select an option

Save scfcode/09a87b49e4558b1d99f7eecec1aacfa2 to your computer and use it in GitHub Desktop.
Ordinal Dates for today, tomorrow, and yesterday... #zsh #today #tomorrow #yesterday #ordinal #date
#!/bin/zsh
# set -x
alias date=/usr/local/opt/coreutils/bin/gdate
today () {
echo $(date --date='today' +%e)
}
tomorrow () {
d=$(date --date='today' +%a)
if [[ $d = "Fri" ]]; then
echo $(date --date='next Monday' +%e)
elif [[ $d = "Sat" ]]; then
echo $(date --date='next Monday' +%e)
selse
echo $(date --date='tomorrow' +%e)
fi
}
yesterday () {
d=$(date --date='today' +%a)
if [[ $d = "Mon" ]]; then
echo $(date --date='last Friday' +%e)
else
echo $(date --date='1 days ago' +%e)
fi
}
ord () {
# d=$(date +%e)
d=$1
case $1 in
1?) d=${d}th ;;
*1) d=${d}st ;;
*2) d=${d}nd ;;
*3) d=${d}rd ;;
*) d=${d}th ;;
esac
echo $d
}
#echo "Yesterday"
yd=$(yesterday)
o=$(ord $yd)
p=$(date "+%b $o, %Y")
echo "Yesterday : [[$p]]"
#echo "Today"
# td=$(today)
# o=$(ord $td)
# p=$(date "+%b $o, %Y")
# echo "[[$p]]"
#echo "Tomorrow"
tm=$(tomorrow)
o=$(ord $tm)
p=$(date "+%b $o, %Y")
echo "Tomorrow : [[$p]]"
# today
# echo "o =: $o"
# brew install coreutils
# /usr/local/opt/coreutils/bin/gdate --date='1 days ago' +%e
# /usr/local/opt/coreutils/bin/gdate --date='next Monday' +%e
# /usr/local/opt/coreutils/bin/gdate --date='last Friday' +%e
# /usr/local/opt/coreutils/bin/gdate --date='today' +%e
# https://www.gnu.org/software/coreutils/manual/html_node/Relative-items-in-date-strings.html
# https://www.gnu.org/software/coreutils/manual/html_node/Date-conversion-specifiers.html#Date-conversion-specifiers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment