Bash diff date doesn't work

Hi everyone,

I've an issue trying to soustracte two dates, e.g:

d1=$(date -d "Nov 18, 2017" +%s)
d2=$(date +%s) # Today we are 2017-11-16

echo "$(( (d1 - d2) / 86400 ))"

Output:

1

I don't understand why it doesn't work. for me, it should give "18 - 16 = 2".

Much appreciated to help me :slight_smile:

I've solved my issue doing this:

d2=$(date -d "$(date +'%Y%m%d')" +%s)

But other proposition will be appreciated, if it exists..

The timestamps aren't 2 or more days apart yet, only 1.6 days. One counts from midnight while the other counts from present time.

Your proposition works by forcing both times to start from midnight. You could also add 86399 to d2 before division, which will force the value to 'round up'.

1 Like