Method of Countdown Date Using bash

  • 2021-07-18 09:22:00
  • OfStack

Do you need to know how many days before an important event? Let the Linux bash and date commands help you!

With the upcoming important holidays, you may need to remind you how long you have to prepare.

Fortunately, you can get a lot of help from the date command. In this article, we'll examine how the date and bash scripts tell you how many days there are between today and your expected events.

First, there are a few hints before proceeding. The% j option of the date command displays the current date as a number between 1 and 366. As you might expect, January 1 will appear as 1, and December 31 will appear as 365 or 366, depending on whether it is a leap year or not. Keep trying. You should see the following:


$ date +%j
339

However, you can get the number of any day in a year in the date command in the following way:


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

Remember, even if the date is past, the above command will show you the date of the current year. However, you can fix the problem by adding years to the command:


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

In leap years, April 29th will be 120 days of a year instead of 119 days.

If you want to count down the days before Christmas and don't want to leave fingerprints on the calendar, you can use the following script:


#!/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 December 25th and today's date, and then subtract them. If the result is a positive number, we will display the remaining days. If it is zero, the message "Merry Christmas" is emitted, and if it is negative, it only tells the person running the script that they missed the holiday. Maybe they're addicted to eggnog.

case statements consist of statements used to print information. When the remaining time is equal to 0, or any number or number beginning with a-sign (that is, past), different information is printed.

You can use the same method for any date that people want to pay attention to. In fact, we can ask the person running the script to provide a date, and then let them know how many days are left between now and that day. This script looks like this.


#!/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 with using this script is that people who run it will be disappointed if they want to know how many days are left until this special day in the second year. Even if they provide a year when they enter the date, date -d The command will still provide only the number of days in this year, not the number of days between now and then.

Calculating the number of days between today and the date of a certain year can be tricky. You need to include all intermediate years and pay attention to those leap years.

Use Unix epoch time

Another way to calculate the number of days between now and a particular date is by using the Unix system to store dates. If you convert the number of seconds from January 1, 1970 to days, you can easily do this, as shown in the following 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"

In explanation 1, 86400 is the number of seconds in a day. Divide the number of seconds since the beginning of the Unix era by the number of days.


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

Summarize


Related articles: