Need to know how many days there are before some important event? Let Linux bash and the date command help with that! 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 Related content how-to How to find files on Linux There are many options you can use to find files on Linux, including searching by file name (or partial name), age, owner, group, size, type and inode number. By Sandra Henry Stocker Jun 24, 2024 8 mins Linux opinion Linux in your car: Red Hat’s milestone collaboration with exida With contributions from Red Hat and critical collaborators, the safety and security of automotive vehicles has reached a new level of reliability. By Sandra Henry Stocker Jun 17, 2024 5 mins Linux how-to How to print from the Linux command line: double-sided, landscape and more There's a lot more to printing from the Linux command line than the lp command. Check out some of the many available options. By Sandra Henry Stocker Jun 11, 2024 6 mins Linux how-to Converting between uppercase and lowercase on the Linux command line Converting text between uppercase and lowercase can be very tedious, especially when you want to avoid inadvertent misspellings. Fortunately, Linux provides a handful of commands that can make the job very easy. By Sandra Henry Stocker Jun 07, 2024 5 mins Linux PODCASTS VIDEOS RESOURCES EVENTS NEWSLETTERS Newsletter Promo Module Test Description for newsletter promo module. Please enter a valid email address Subscribe