Computing Days Between Dates with bash

Needed a quick way to compute the number of days between two dates:

!/bin/bash
# adapted from https://www.linuxforfreshers.com/2019/01/how-to-count-days-since-specific-date.html
 if [ $# -ne 2 ]; then
     echo "USAGE: $(basename $0) start_date end_date";
     exit 1;
 else
   start=$1;
   end=$2;
   echo "$((($(date +%s --date "$end") - $(date +%s --date "$start"))/(3600*24))) days between $start and $end";
 fi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s