Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»Calculate difference between two dates in Bash

    Calculate difference between two dates in Bash

    By RahulMarch 2, 20232 Mins Read

    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.

    Advertisement

    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:

    1
    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:

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

    The output of this command will be:

    1
    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:

    1
    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:

    1
    2
    3
    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:

    1
    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:

    1
    2
    3
    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.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    How to Generate Random String in Bash

    Efficiently Reading a File Line by Line in a Shell Script

    View 1 Comment

    1 Comment

    1. Reini on March 1, 2023 11:27 am

      Why do I need “| bc” or “| awk”. Everything is done/calculated before.

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Python Lambda Functions – A Beginner’s Guide
    • 10 Practical Use Cases for Lambda Functions in Python
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.