# Quickly calculate date differences

I often want to make some quick date calculations, such as:

• What is the difference between these two dates?
• What is the date n weeks after this other date?

I usually open a calendar and count the days, but I think there should be a program/script that I can use to do these kinds of calculations. Any suggestions?

The "n weeks after a date" is easy with GNU date(1):

\$ date -d 'now + 3 weeks'
Tue Dec  6 23:58:04 EST 2011
\$ date -d 'Aug 4 + 3 weeks'
Thu Aug 25 00:00:00 EST 2011
\$ date -d 'Jan 1 1982 + 11 weeks'
Fri Mar 19 00:00:00 EST 1982

I don't know of a simple way to calculate the difference between two dates, but you can wrap a little logic around date(1) with a shell function.

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

Swap d1 and d2 if you want the date calculation the other way, or get a bit fancier to make it not matter. Furthermore, in case there is a non-DST to DST transition in the interval, one of the days will be only 23 hours long; you can compensate by adding ½ day to the sum.

echo \$(( (((d1-d2) > 0 ? (d1-d2) : (d2-d1)) + 43200) / 86400 )) days

For a set of portable tools try my very own dateutils. Your two examples would boil down to one-liners:

ddiff 2011-11-15 2012-04-11
=>
148

or in weeks and days:

ddiff 2011-11-15 2012-04-11 -f '%w %d'
=>
21 1

and

=>
2012-04-10
• +1 your tools rock (though dateadd -i '%m%d%Y' 01012015 +1d doesn't seem to work, it just hangs there indefinitely... it does work if the date specs are separated by a char, any char... any idea what's wrong ?) – don_crissti Nov 22 '15 at 2:17
• @don_crissti The parser couldn't distinguish between numerals-only dates and durations, it's fixed in the current master (d0008f98) – hroptatyr Nov 23 '15 at 6:21
• Do you have window's binary distrib of dateutils? – mosh Feb 12 '17 at 4:18
• @mosh no, and I don't have the facilities to try. – hroptatyr Feb 12 '17 at 8:21
• Late to the party here, but dateutils is on cygwin if you need to use on Windows. – gregb212 Mar 5 '19 at 21:32

A python example for calculating the number of days I've walked the planet:

\$ python
>>> from datetime import date as D
>>> print (D.today() - D(1980, 6, 14)).days
11476
• Just in case someone wants this to behave just like a single command, instead of typing in an interactive interpreter : ychaouche@ychaouche-PC ~ \$ python -c "from datetime import date as d; print (d.today() - d(1980, 6, 14)).days" 12813 ychaouche@ychaouche-PC ~ \$ – ychaouche Jul 14 '15 at 14:56
• this works for me python3 -c "from datetime import date as d; print (d.today() - d(2016, 1, 9))" days at the end is not required – Kiran K Telukunta Oct 14 '16 at 5:18

I usually prefer having the time/date in unix utime format (number of seconds since the epoch, when the seventies begun, UTC). That way it always boils down to plain subtraction or addition of seconds.

The problem the usually becomes transforming a date/time into this format.

In a shell environment/script you can get it with date '+%s' At the time of writing, the current time is 1321358027.

To compare with 2011-11-04 (my birthday), date '+%s' -d 2011-11-04, yielding 1320361200. Subtract: expr 1321358027 - 1320361200 gives 996827 seconds, which is expr 996827 / 86400 = 11 days ago.

Converting from utime (1320361200 format) into a date is very simple to do in for instance C, PHP or perl, using standard libraries. With GNU date, the -d argument can be prepended with @ to indicate "Seconds since the Epoch" format.

If a graphical tool is OK for you, I heartily recommend qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, IIRC). There you can say e.g.

days(1900-05-21, 1900-01-01)

to get the number of days (140, since 1900 was not a leap year) between the dates, but of course you can also do the same for times:

17:12:45 − 08:45:12

yields 8.4591667 hours or, if you set the output to time formatting, 8:27:33.

• That's great, and even more so because qalculate does have a CLI. Try qalc, then help days. – Sparhawk Jul 4 '14 at 12:19
• qcalc example: qalc -t 'days(1900-05-21, 1900-01-01)' -> 140 – sierrasdetandil Mar 25 '19 at 15:39

This came up when using date -d "\$death_date - \$y years - \$m months - \$d days" to get a birth date (for genealogy). That command is WRONG. Months aren't all the same length, so (date + offset) - offset != date. Ages, in year/month/day, are measures going forwards from the date of birth.

\$ date --utc -d 'mar 28 1867 +72years +11months +2days'
Fri Mar  1 00:00:00 UTC 1940

\$ date --utc -d 'mar 1 1940 -72years -11months -2days'
Sat Mar 30 00:00:00 UTC 1867
# (2 days later than our starting point)

Date gives the correct output in both cases, but in the second case you were asking the wrong question. It matters WHICH 11 months of the year the +/- 11 cover, before adding/subtracting days. For example:

\$ date --utc -d 'mar 31 1939  -1month'
Fri Mar  3 00:00:00 UTC 1939
\$ date --utc -d 'mar 31 1940  -1month' # leap year
Sat Mar  2 00:00:00 UTC 1940
\$ date --utc -d 'jan 31 1940  +1month' # leap year
Sat Mar  2 00:00:00 UTC 1940

For subtracting to be the inverse operation of adding, the order of operations would have to be reversed. Adding adds years, THEN months, THEN days. If subtracting used the opposite order, then you'd get back to your starting point. It doesn't, so you don't, if the days offset crosses a month boundary in a different length month.

If you need to work backwards from an end date and age, you could do it with multiple invocations of date. First subtract the days, then the months, then the years. (I don't think it's safe to combine the years and months in a single date invocation, because of leap years altering the length of February.)

Another way to calculate the difference between two dates of the same calendar year you could use this:

date_difference.sh
1  #!/bin/bash
2  DATEfirstnum=`date -d "2014/5/14" +"%j"`
3  DATElastnum=`date -d "12/31/14" +"%j"`
4  DAYSdif=\$((\$DATElastnum - \$DATEfirstnum))
5  echo "\$DAYSdif"
• Line 1 declares to the shell which interpreter to use.
• Line 2 assigns the value from the out of date to the variable DATEfirstnum. The -d flag displays the string in a time format in this case May 14th 2014 and +"%j" tells date to format the output to just the day of the year (1-365).
• Line 3 is the same as Line 2 but with a different date and different format for the string, December 31st, 2014.
• Line 4 assigns the value DAYSdif to the difference of the two days.
• Line 5 displays the value of DAYSdif.

This works with the GNU version of date, but not on the PC-BSD/FreeBSD version. I installed coreutils from ports tree and used the command /usr/local/bin/gdate instead.

• This script will not run. There is a typo on the last line and spaces around the variable assignments, so bash is attempting to run a program called DATEfirst name with two arguments. Try this: DATEfirstnum=\$(date -d "\$1" +%s) DATElastnum=\$(date -d "\$2" +%s) Also, this script will not be able to calculate the difference between two different years. +%j refers to day of year (001..366) so ./date_difference.sh 12/31/2001 12/30/2014 outputs -1. As other answers have noted you need to convert both dates into seconds since 1970-01-01 00:00:00 UTC. – Six Feb 28 '15 at 13:29
• You don't need \$ inside arithmetic expression: \$((DATElastnum - DATEfirstnum)) will also work. – Ruslan Jun 2 '18 at 14:26

With the help of dannas solutions this can be done in one line with following code:

python -c "from datetime import date as d; print(d.today() - d(2016, 7, 26))"

(Works in both Python 2.x and Python 3.)

I frequently use SQL for date calculations. For example MySQL, PostgreSQL or SQLite:

bash-4.2\$ mysql <<< "select datediff(current_date,'1980-06-14')"
datediff(current_date,'1980-06-14')
11477

bash-4.2\$ psql <<< "select current_date-'1980-06-14'"
?column?
----------
11477
(1 row)

bash-4.2\$ sqlite2 <<< "select julianday('now')-julianday('1980-06-14');"
11477.3524537035

Other times I just feel in mood for JavaScript. For example SpiderMonkey, WebKit, Seed or Node.js:

bash-4.2\$ js -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.477526192131

bash-4.2\$ jsc-1 -e 'print((new Date()-new Date(1980,5,14))/1000/60/60/24)'
11477.47757960648

bash-4.2\$ seed -e '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11477.4776318287

bash-4.2\$ node -pe '(new Date()-new Date(1980,5,14))/1000/60/60/24'
11624.520061481482

(Watch out when passing the month to the JavaScript Date object's constructor. Starts with 0.)

date and bash can do date differences (OS X options shown). Place the latter date first.

echo \$(((\$(date -jf%D "04/03/16" +%s) - \$(date -jf%D "03/02/16" +%s)) / 86400))
# 31

There's also GNU unit's time calculations combined with GNU date:

\$ gunits \$(gdate +%s)sec-\$(gdate +%s -d -1234day)sec 'yr;mo;d;hr;min;s'
3 yr + 4 mo + 16 d + 12 hr + 37 min + 26.751072 s
\$ gunits \$(gdate +%s -d '2015-1-2 3:45:00')sec-\$(gdate +%s -d '2013-5-6 7:43:21')sec 'yr;mo;d;hr;min;s'
1 yr + 7 mo + 27 d + 13 hr + 49 min + 26.206759 s

(gunits is units in Linux, gdate is date)

datediff.sh on github:gist

#!/bin/bash
#Simplest calculator two dates difference. By default in days

# Usage:
# ./datediff.sh first_date second_date [-(s|m|h|d) | --(seconds|minutes|hours|days)]

first_date=\$(date -d "\$1" "+%s")
second_date=\$(date -d "\$2" "+%s")

case "\$3" in
"--seconds" | "-s") period=1;;
"--minutes" | "-m") period=60;;
"--hours" | "-h") period=\$((60*60));;
"--days" | "-d" | "") period=\$((60*60*24));;
esac

datediff=\$(( (\$first_date - \$second_date)/(\$period) ))
echo \$datediff

camh's answer takes care of most of it, but we can improve to deal with rounding, time zones, etc., plus we get some extra precision and the ability to pick our units:

datediff() {
#convert dates to decimal seconds since 1970-01-01 00:00:00 UTC
date1seconds=\$(date +%s.%N -d "\$date1")
date2seconds=\$(date +%s.%N -d "\$date2")

#Calculate time difference in various time units
timeseconds=\$(printf "%0.8f\n" \$(bc <<<"scale=9; (\$date2sec-\$date1sec)"))
timeminutes=\$(printf "%0.8f\n" \$(bc <<<"scale=9; (\$date2sec-\$date1sec)/60"))
timehours=\$(printf "%0.8f\n" \$(bc <<<"scale=9; (\$date2sec-\$date1sec)/3600"))
timedays=\$(printf "%0.8f\n" \$(bc <<<"scale=9; (\$date2sec-\$date1sec)/86400"))
timeweeks=\$(printf "%0.8f\n" \$(bc <<<"scale=9; (\$date2sec-\$date1sec)/604800"))
}

-d tells date that we're supplying a date-time to convert. +%s.%N changes the date-time to seconds.nanoseconds since 1970-01-01 00:00:00 UTC. bc calculates the difference between the two numbers, and the dividing factor gets us the different units. printf adds a 0 before the decimal place if needed (bc does not) and ensures rounding goes to nearest (bc just truncates). You could also use awk.

Now let's run this with a funky test case,

date1='Tue Jul  9 10:18:04.031 PST 2020'
date2='Wed May  8 15:19:34.447 CDT 2019'
datediff "\$date1" "\$date2"

echo \$timeseconds seconds
-36971909.584000000 seconds

echo \$timeminutes minutes
-616198.493066667 minutes

echo \$timehours hours
-10269.974884444 hours

echo \$timedays days
-427.915620185 days

echo \$timeweeks weeks
-61.130802884 weeks

Note that since the length of a month or year isn't always the same, there isn't a single "correct" answer there, though one could use 365.24 days as a reasonable approximation nowadays.

You can use the awk Velour library:

velour -n 'print t_secday(t_utc("2017-4-12") - t_utc("2017-4-5"))'

Or:

velour -n 'print t_secday(t_utc(ARGV[1]) - t_utc(ARGV[2]))' 2017-4-12 2017-4-5

Result:

7