How to Get the Difference Between Two Dates Under Bash

How to find the difference in days between two dates?

If you have GNU date, it allows to print the representation of an arbitrary date (-d option).
In this case convert the dates to seconds since EPOCH, subtract and divide by 24*3600.

Example using GNU date (from https://stackoverflow.com/a/9008871/215713):

let DIFF=($(date +%s -d 20210131)-$(date +%s -d 20210101))/86400
echo $DIFF
30

This also works with dates in other formats, for example "2021-01-31".

Other answers suggest ways to do it that don't require GNU date.

How do I get the difference between two dates under bash


let DIFF=(`date +%s -d 20120203`-`date +%s -d 20120115`)/86400
echo $DIFF

Shell script to get difference between two dates

Using only date and shell arithmetics:

echo $((($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s")) / 86400))

Find difference between two dates in bash

You can use:

date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
date -d @$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) )) -u +'%H:%M:%S'

And the output is:

23:00:00

However, for arbitrary differences over 24 hours, you have to start worrying about how the value for days will be represented. Negative values will also present problems.

The problem with this is that if you drop the format string, the date presented is:

Thu Jan  1 23:00:00 UTC 1970

So, while this works for the two given date/time values, it is not really a general solution.

Consider adding %j for the day of year; it will work semi-sanely for up to 1 year's difference.

Otherwise, capture the difference in a variable and present the results with a separate calculation.

date1="Sat Dec 28 03:22:19 2013"
date2="Sun Dec 29 02:22:19 2013"
delta=$(( $(date -d "$date2" +%s) - $(date -d "$date1" +%s) ))
if [[ $delta -lt 0 ]]
then sign="-"; delta=${delta/^-/}
else sign="+"
fi
ss=$(( $delta % 60 ))
delta=$(( $delta / 60 ))
mm=$(( $delta % 60 ))
delta=$(( delta / 60 ))
hh=$(( $delta % 24 ))
dd=$(( $delta / 24 ))
printf "$sign%d %.2d:%.2d:%.2d\n" $dd $hh $mm $ss

Output:

+0 23:00:00

(Meaning: a positive difference of 0 days, 23 hours, 0 minutes, 0 seconds.) Splitting a number of days such as 1000 into years, months and days is fraught without any reference dates. Here, there are reference dates to help, but it would still be ... fun.

bash difference between 2 dates in bash by yyyy-mm-dd in N days

Expanding on my comment:

see the accepted answer for Quickly calculate date differences

The person who answered that question wrote a custom function called datediff:

$ datediff() {
d1=$(date -d "$1" +%s)
d2=$(date -d "$2" +%s)
echo $(( (d1 - d2) / 86400 )) days
}

We can use this same function to compare the 2 dates from the question:

$ START=$(date --date=yesterday +%F)
$ END=$(date --date "2 days ago" +%F)
$ echo "${START} : ${END}"
2019-12-30 : 2019-12-29
$ datediff "${START}" "${END}"
1 days

Calculate difference in months between two dates in Unix?

How about:

#!/bin/bash

DATE1=201712
DATE2=201801

y1=${DATE1:0:4}
m1=${DATE1:4:2}

y2=${DATE2:0:4}
m2=${DATE2:4:2}

diff=$(( ($y2 - $y1) * 12 + (10#$m2 - 10#$m1) ))
echo $diff

Time difference in seconds between given two dates

Assuming GNU implementation based OS, you can use date's option %s and -d to calculate the time difference in seconds using command substitution and arithmetic operations.

START="2019-01-05 23:52:00"
END="2019-01-06 00:02:10"

Time_diff_in_secs=$(($(date -d "$END" +%s) - $(date -d "$START" +%s)))
echo $Time_diff_in_secs

Output:

610

Hope this helps!!!



Related Topics



Leave a reply



Submit