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?

Asked By: daniel kullmann

||

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.

Answered By: MattBianco

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
Answered By: Daniel Näslund

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'  # Note: answer should be 92 days but in my timezone, DST starts between the dates.
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
Answered By: camh

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.)

Answered By: manatwork

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

dadd 2011-11-15 21w
=>
  2012-04-10

Important note regarding Ubuntu installation:

These very same dateutils are available as a Ubuntu package, and hence installable through sudo apt install dateutils.

However, commands need to be preceded by a dateutils. prefix, as in dateutils.ddiff 2019-03-28 2019-05-16

They are also available in Fedora and in Fedora EPEL for RHEL or CentOS with sudo dnf install dateutils. In Fedora, these packages do not require a prefix but use the long names — e.g, datediff and dateadd instead of ddiff and dadd.

For Arch Linux, install the package with sudo pacman -Sy dateutils. The binaries are called datediff and dateadd, just like in Fedora.

Answered By: hroptatyr

You can use qalculate (a calculator with an emphasis on unit conversions, it comes with a GTK and KDE interface, and a command line tool qalc). 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.

For the command line, valid examples are:

qalc -t 'days(1900-05-21, 1900-01-01)'
qalc -t '"1900-05-21" - "1900-01-01"'
qalc -t 'today - "1900-01-01"'

(Thank you Sparhawk, sierrasdetandil, Denilson Sá Maia for the helpful comments, I added your content to this answer.)

Answered By: quazgar

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.

Answered By: Justin Holcomb

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.)

Answered By: Peter Cordes

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)

Answered By: Larry

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
Answered By: user178063

date -d is specific to GNU Date and is not defined by POSIX:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/date.html

As others have mentioned, this is something that might be better suited to a
proper programming language. For example PHP:

<?php
$o1 = date_create('2020-02-26');
$o2 = date_create('2020-02-16');
$o3 = date_diff($o2, $o1);
echo 'days: ', $o3->d, "n";

Result:

days: 10
Answered By: Zombo

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.)

Answered By: Kiran K Telukunta

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
Answered By: Dave

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.8fn" $(bc <<<"scale=9; ($date2sec-$date1sec)"))
timeminutes=$(printf "%0.8fn" $(bc <<<"scale=9; ($date2sec-$date1sec)/60"))
  timehours=$(printf "%0.8fn" $(bc <<<"scale=9; ($date2sec-$date1sec)/3600"))
   timedays=$(printf "%0.8fn" $(bc <<<"scale=9; ($date2sec-$date1sec)/86400"))
  timeweeks=$(printf "%0.8fn" $(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.

Answered By: TTT

This is a rather old thread, but I discovered something interesting in BusyBox, at least pertaining to adding and subtracting N units of time from a known date. Not so much for going the other way to determine the interval between two known dates though.

I’m stuck on an embedded system with BusyBox and the super handy now - 10 days notation fails there. In fact, a very limited set of input formats is available for the -d option.

$ busybox date --help
BusyBox v1.19.0 (2019-07-14 10:52:19 UTC) multi-call binary.

Usage: date [OPTIONS] [+FMT] [TIME]

Display time (using +FMT), or set time

        [-s,--set] TIME Set time to TIME
        -u,--utc        Work in UTC (don't convert to local time)
        -R,--rfc-2822   Output RFC-2822 compliant date string
        -I[SPEC]        Output ISO-8601 compliant date string
                        SPEC='date' (default) for date only,
                        'hours', 'minutes', or 'seconds' for date and
                        time to the indicated precision
        -r,--reference FILE     Display last modification time of FILE
        -d,--date TIME  Display TIME, not 'now'
        -D FMT          Use FMT for -d TIME conversion

Recognized TIME formats:
        hh:mm[:ss]
        [YYYY.]MM.DD-hh:mm[:ss]
        YYYY-MM-DD hh:mm[:ss]
        [[[[[YY]YY]MM]DD]hh]mm[.ss]

However, some math can be done on an input date. Given those formats, I found setting the day number of the input date to zero gives you the last day of the previous month:

$ date -d 2020.03.00-12:00
Sat Feb 29 12:00:00 EST 2020 

Further, inputting negative numbers keeps subtracting backwards, which I certainly did not expect:

$ date -d 2020.03.-10-12:00
Wed Feb 19 12:00:00 EST 2020

So you can do some quick math on your known date to generate values to feed into the -d option in the BusyBox date call:

#!/bin/bash
#Other shells not tested.

start_year=2020
start_month=1
start_day=20

offset_year=-2
offset_month=4
offset_day=5

new_year=$(( $start_year + $offset_year ))
new_month=$(( $start_month + $offset_month ))
new_day=$(( $start_day + $offset_day ))

new_date$( busybox date -d $new_year.$new_month.$new_day-12:00 +%Y-%m-%d )
echo $new_date

Output:

2018-05-25

Can anyone confirm if this also works with GNU, BSD, or MacOS date? I certainly appreciate that the GNU now - 10 days is very much better, but I’m interested in making this as portable as possible. I have confirmed that Android is very different (neither approach works there).

Answered By: Nate

This is a rather old thread, but I discovered something interesting. I’m stuck on an embedded system with BusyBox (v1.19) and was a bit disappointed that the super handy now - 10 days notation fails there. In fact, a very limited set of input formats is available for the -d option.

Recognized TIME formats:
    hh:mm[:ss]
    [YYYY.]MM.DD-hh:mm[:ss]
    YYYY-MM-DD hh:mm[:ss]
    [[[[[YY]YY]MM]DD]hh]mm[.ss]

However, some math can be done on an input date.

Given those formats, I found setting the day of the date to display to zero gives you the last day of the previous month:

$ date -d 2020.03.00-12:00
Sat Feb 29 12:00:00 EST 2020 

And inputting negative numbers goes back even further:

$ date -d 2020.03.-10-12:00
Wed Feb 19 12:00:00 EST 2020

Can anyone confirm if this also works on GNU date? (Although the now - 10 days is very much better)

Answered By: Nate

For a simple diff of days we can use the command date:

$ dateA="2020-11-10"
$ dateB="2020-12-28"
$ echo "(`date -d $dateB +%s` - `date -d $dateA +%s`)/86400" | bc
48
  • -d, --date=STRING display time described by STRING
  • %s seconds since 1970-01-01 00:00:00 UTC
  • 86400 represents the amount of seconds in a day

The echo command will just build a string with the desired math expression, then the bc will resolve it.

For add n weeks i think the @camh’s answer is a great approach:

$ dateA="2020-11-10"
$ date -d "$dateA + 3 weeks"
ter dez  1 00:00:00 -03 2020

Furthermore, you can change the weeks for days, months, years, etc.

Answered By: Daniel da Rosa

units is my favourite cli calculator. It is dimensioned, and knows a lot of conversions to boot:

$ units 135J/45N
    Definition: 3 m

Time? sure

$ units 1628675579s time
    51 year + 223 day + 1 hr + 25 min + 54.291401 sec

It does know about months, but as has been pointed out, it’s dodgy to use months in period measurements.

$ units 1628675579s 'year;month;day;hr;min;sec'
    51 year + 7 month + 10 day + 2 min + 27.472839 sec

Oh, and don’t forget that it’s a calculator foremost, not a conversion tool.

$ units 1628675579s-1628600000s hms
    20 hr + 59 min + 39 sec
Answered By: bobbogo
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.