Americas

  • United States
sandra_henrystocker
Unix Dweeb

Counting down the days in Linux using bash

How-To
Dec 09, 20194 mins
Linux

Need to know how many days there are before some important event? Let Linux bash and the date command help with that!

clock superimposed on monthly calendar
Credit: Thinkstock

With some pretty important holidays right around the corner, you might need to be reminded how much longer you have to prepare.

Fortunately, you can get a lot of help from the date command. In this post, we’ll look at ways that date and bash scripts can tell you how many days there are between today and some event that you’re anticipating.

First a couple hints at how this is going to work. The date command’s %j option is going to show you today’s date as a number between 1 and 366. January 1st, as you’d expect, will be displayed as 1 and December 31st will be 365 or 366 depending on whether it’s leap year. Go ahead and try it. You should see something like this:

$ date +%j
339

You can, however, get the date command to give you the day-of-the-year number for any date by supplying it in a date command like this:

$ date -d "Mar 18" +%j
077

One important thing to keep in mind is that this command will show you the date in the current year even if that date is in the past. However, you can add a year to the command and fix that:

$ date -d "Apr 29" +%j
119
$ date -d "Apr 29 2020" +%j
120

In a leap year, Apr 29th will be the 120th day of the year, not the 119th.

If you want to count down the days until Christmas and don’t want to end up with fingerprints on your wall calendar, you can use a script like this:

#!/bin/sh

XMAS=`date -d "Dec 25" +%j`
TODAY=`date +%j`
DAYS=$(($XMAS - $TODAY))

case $DAYS in
  0) echo "It's today! Merry Christmas!";;
  [0-9]*) echo "$DAYS days remaining";;
  -[0-9]*) echo "Oops, you missed it";;
esac

In this script, we get the day numbers for Dec 25th and today and then subtract one from the other. If the result is positive, we display the number of days remaining. If it’s zero, we issue a “Merry Christmas” message and, if it’s negative, we just tell the person running the script that they missed the holiday. Maybe they got carried away with the egg nog.

The case statement is made up of ready-to-print statements for remaining-day counts that equal zero, those that that include any digits and those that start with a sign (i.e., in the past).

The same idea can be generalized for any date that someone wants to keep in focus. In fact, we can ask the person running our script to supply the date and then let them know how many days remain between now and then. This version of the script does just that.

#!/bin/sh

echo -n "Enter event date (e.g., June 6): "
read dt
EVENT=`date -d "$dt" +%j`
TODAY=`date +%j`
DAYS=`expr $EVENT - $TODAY`

case $DAYS in
  0) echo "It's today!";;
  [0-9]*) echo "$DAYS days remaining";;
  -[0-9]*) echo "Oops, you missed it";;
esac

One problem you’ll have with this script is that, if the person running it is hoping to find out how many days must pass before they reach a special day the following year, they’ll be disappointed. Even if they supply a year when they enter the date, the date -d command will still only supply the day number in that year, not the days between now and then.

Calculating the number of days between today and some date years from now can be a bit tricky. You’d need to include all the intervening years and pay attention to those that are leap years.

Using Unix (Epoch) time

Another way to calculate the number of days between now and some special date is to take advantage of the way that Unix systems store dates. If you convert the number of seconds since the start of the day on Jan 1, 1970 to days, you can do this quite easily as in this script:

#!/bin/bash

echo -n "Enter target date (e.g., Mar 18 2021)> "
read target_date
today=`echo $(($(date --utc --date "$1" +%s)/86400))`
target=`echo $(($(date --utc --date "$target_date" +%s)/86400))`
days=`expr $target - $today`
echo "$days days until $target_date"

To explain, 86400 is the number of seconds in a day. Dividing the number of seconds since the epoch began by this number gives us the number of days.

$ ./countdown
Enter target date (e.g., Mar 18 2021)> Mar 18 2020
104 days until Mar 18 2020
sandra_henrystocker
Unix Dweeb

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author