Skip to content

Calculate difference between two dates in Bash

On this page

In Bash, you can easily calculate the difference between two dates. This can be useful in various scenarios, such as calculating the number of days between two events or determining the age of a person. In this article, we’ll discuss different methods to calculate the difference between two dates in Bash.

Calculate difference between two dates

The ‘date‘ command in Bash can be used to calculate the difference between two dates. The syntax for using the ‘date’ command is as follows:


echo $((($(date -d "YYYY-MM-DD" +%s) - $(date -d "YYYY-MM-DD" +%s))/86400))

In the above command, replace “YYYY-MM-DD” with the two dates you want to compare in the same format. The command will output the number of days between the two dates.

Let’s look at an example. If you want to calculate the number of days between January 1, 2023 and February 28, 2023, you can use the following command:


echo $((($(date -d "2023-02-28" +%s) - $(date -d "2023-01-01" +%s))/86400))

# Output: 58

This means that there are 58 days between January 1, 2023 and February 28, 2023.

Method 2: Using the ‘bc’ command

The ‘bc’ command in Bash can be used to perform mathematical calculations. To calculate the difference between two dates using the ‘bc’ command, use the following syntax:


echo \($(date -d "YYYY-MM-DD" +%s) - $(date -d "YYYY-MM-DD" +%s)\) / 86400 | bc	 

In the above command, replace “YYYY-MM-DD” with the two dates you want to compare in the same format. The command will output the number of days between the two dates.

For example:


echo \($(date -d "2023-02-28" +%s) - $(date -d "2023-01-01" +%s)\) / 86400 | bc

# Output: 58

Method 3: Using the ‘awk’ command

The ‘awk’ command in Bash can also be used to calculate the difference between two dates. Use the following syntax:


echo $(date -d "YYYY-MM-DD" +%s) $(date -d "YYYY-MM-DD" +%s) | awk '{print ($1 - $2) / 86400}' 

]

In the above command, replace “YYYY-MM-DD” with the two dates you want to compare in the same format. The command will output the number of days between the two dates.

For example:


echo $(date -d "2023-02-28" +%s) $(date -d "2023-01-01" +%s) | awk '{print ($1 - $2) / 86400}'

# Output: 58

Conclusion

Calculating the difference between two dates in Bash can be done using different commands, such as ‘date’, ‘bc’, and ‘awk’. By using these commands, you can easily calculate the number of days between two dates. Knowing how to calculate the difference between two dates can be useful in various scenarios, such as calculating the age of a person or the number of days between two events.

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top