Today I Learned

A Hashrocket project

Relative Dates with GNU date

GNU date ships with the ability to add and subtract dates. Using the -d flag one can add add or subtract years, months, days, weeks, and seconds. As an example here’s a future date helper:

in() {
  if [ "$(uname)" == "Darwin" ]; then
    gdate -d"$(gdate) +$1 $2" "+%Y-%m-%d"
  else
    date -d"$(date) +$1 $2" "+%Y-%m-%d"
  fi
}
~❯ in 7 days
2018-03-24
~❯ in 2 months
2018-05-17

This can be handy for some CLI utilities like Todo.txt.

For example:

~❯ t add Post first TIL due:$(in 3 days)
10 Post first TIL due:2018-03-20
TODO: 10 added.

Note: If you’re on a Mac you’ll need to install the GNU coreutils. By default all commands will be prepended with g, hence gdate above.

brew install coreutils

Check the man pages for more info.