How long ago was this?
Bash + GNU utilities, 37
tr , \\n|date -f- +%s|dc -e??-86400/p
tr
replaces the comma with a newline. date
reads the newline separated dates and outputs the number of seconds since the Unix epoch that the passed-in date represents. These numbers are then put on the dc
stack. Then its a simple matter of subtraction and divide by (24*60*60). In this case, dc
stack-based RPN arithmetic evaluation is better than bc
or bash $( )
, mostly because the subraction-before-division needs no parentheses.
Input via STDIN:
$ echo 2015-12-3,2015-12-1 | ./longago.sh
2
$ echo 2015-12-3,2014-12-1 | ./longago.sh
367
$ echo 2015-12-3,2013-12-3 | ./longago.sh
730
$
Julia, 67 bytes
print(Int(-diff(map(i->Date(i,"y-m-d"),split(readline(),",")))[1]))
Ungolfed:
# Read a line from STDIN
r = readline()
# Split it into two character dates
s = split(r, ",")
# Convert each to a Date object
d = map(i -> Date(i, "y-m-d"), s)
# Compute the difference in dates (first-second)
f = diff(d)[1]
# Convert the Base.Date.Day object to an integer
# Negate to get second-first
i = Int(-f)
# Print to STDOUT
print(i)
Scala, 166 139 120 116 92 bytes
print(args(0).replace('-','/').split(",").map(java.util.Date.parse(_)/86400000).reduce(_-_))
Usage: scala [source filename].scala [date1],[date2]
Note: The third version (120 bytes) and on uses a deprecated API. It still compiles and works fine. Note2: Thanks to the commenters below for the great advice!