How to count down the date using bash

How to count down the date using bash

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

With a major holiday coming up, you may need a reminder of how long you have to prepare.

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

First, a few tips 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 be displayed as 1, and December 31 will be displayed as 365 or 366, depending on whether it is a leap year. Keep trying. You should see something like this:

$ date +%j
339

However, you can get the day of the year as a number using the date command as follows:

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

Keep in mind that the above command will show you the current year's date, even if that date is in the past. However, you can fix this by adding the year to the command:

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

In a leap year, April 29th would be 120 days in a year instead of 119 days.

If you want to count down the days until Christmas without leaving fingerprints on your calendar, you can use this 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 take December 25th and today's date, and then subtract them. If the result is positive, we display the number of days remaining. If it's zero, then a "Merry Christmas" message is sent out, if it's negative, then just tell the person running the script that they're missing the holiday. Maybe they were addicted to eggnog.

The case statement consists of statements used to print information when the remaining time is equal to 0, or any number or a number starting with a - sign (that is, in the past).

The same approach can be used for any date people want to focus on. We can actually ask the person running the script to provide a date and then let them know how many days are left between now and that date. The script is this.

#!/bin/sh
echo -n "Enter event date (eg, 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 run into with this script is that if someone running it expects to know how many days are left until that special day the following year, they'll be disappointed. Even if they provide the year when they enter the date, date -d command will only give the number of days in the year, not the number of days between now and then.

Calculating the number of days between today and a date in a certain year can be a little tricky. You need to include all the intervening years, and take note of those leap years.

Use Unix epoch time

Another way to calculate the number of days between now and a particular date is to take advantage of the way Unix systems store dates. This can be done easily if we convert the number of seconds since January 1, 1970 to the number of days, as shown in the following script:

#!/bin/bash
echo -n "Enter target date (eg, 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. Divide this number by the number of seconds since the start of the Unix epoch to get the number of days.

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

Summarize

The above is the method of using bash to count down the date introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • Detailed explanation of bash command usage
  • How to List All Bash Shell Builtin Commands Example
  • Detailed explanation of the differences between source, sh, bash, and ./ in shell script execution
  • How to Check if a File Exists Using Bash Shell
  • Tutorial on using Python scripts to implement some Bash Shells in Linux
  • Shell Programming: Bash Spaces

<<:  Binary installation of mysql 5.7.23 under CentOS7

>>:  centos7.2 offline installation mysql5.7.18.tar.gz

Recommend

CSS3 achieves flippable hover effect

CSS3 implements a flippable hover effect. The spe...

Briefly describe mysql monitoring group replication

Original text: https://dev.mysql.com/doc/refman/8...

Use HTML to write a simple email template

Today, I want to write about a "low-tech&quo...

MySQL table addition, deletion, modification and query basic tutorial

1. Create insert into [table name] (field1, field...

The 6 Most Effective Ways to Write HTML and CSS

This article shares the 6 most effective methods,...

Linux uses suid vim.basic file to achieve privilege escalation

Reproduce on Kali First set suid permissions for ...

How to configure Nginx domain name rewriting and wildcard domain name resolution

This article introduces how to configure Nginx to...

CSS3 to achieve dynamic background gradient effect

Learning CSS3 is more about getting familiar with...

Beginners learn some HTML tags (1)

Beginners can learn HTML by understanding some HT...

Detailed process of deploying Docker to WSL2 in IDEA

The local environment is Windows 10 + WSL2 (Ubunt...

MySQL database optimization: index implementation principle and usage analysis

This article uses examples to illustrate the prin...

Loading animation implemented with CSS3

Achieve results Implementation Code <h1>123...