15

If there are dates as 2010-06-01 and another as 2010-05-15

Using shell script or date command how to get the number of days between the two dates

Thanks..

1
  • 1
    python -c'from datetime import date; print(date(2010, 6, 1) - date(2010, 5, 15)).days'
    – jfs
    Aug 2, 2010 at 4:53

5 Answers 5

23

Using only date and shell arithmetics:

echo $((($(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s")) / 86400))
3
  • 1
    It's not necessary to use $(()) twice. Just use a set of grouping parentheses: echo $(( ( $(date -d "2010-06-01" "+%s") - $(date -d "2010-05-15" "+%s") ) / 86400)) (optional spaces added for emphasis and readability) Aug 2, 2010 at 11:05
  • Note that %s is specific to GNU date, so it might not be available on non-Linux systems. The comp.unix.shell FAQ has a long discussion on date calculations (basically, it's hard unless you have GNU date). Aug 2, 2010 at 19:21
  • In fact there's a bigger problem: it sometimes fails in the spring… My answer explains why. Aug 2, 2010 at 19:32
14

There's a solution that almost works: use the %s date format of GNU date, which prints the number of seconds since 1970-01-01 00:00. These can be subtracted to find the time difference between two dates.

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

But the following displays 0 in some locations:

echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s)) / 86400))

Because of daylight savings time, there are only 23 hours between those times. You need to add at least one hour (and at most 23) to be safe.

echo $((($(date -d 2010-03-29 +%s) - $(date -d 2010-03-28 +%s) + 43200) / 86400))

Or you can tell date to work in a timezone without DST.

echo $((($(date -u -d 2010-03-29 +%s) - $(date -u -d 2010-03-28 +%s)) / 86400))

(POSIX says to call the reference timezone is UTC, but it also says not to count leap seconds, so the number of seconds in a day is always exactly 86400 in a GMT+xx timezone.)

1
  • Your third example actually yields 82800 because you misgrouped the expression and are actually adding 0 due to the higher precedence of /, can you fix the typo? May 8, 2013 at 12:06
6

OSX date is different than GNU date. Got it working like this in OSX. This is not portable solution.

start_date=$(date -j -f "%Y-%m-%d" "2010-05-15" "+%s")
end_date=$(date -j -f "%Y-%m-%d" "2010-06-01" "+%s")

echo $(( ($end_date - $start_date) / (60 * 60 * 24) ))

Idea is still same as in the other answers. Convert dates to epoch time, subtract and convert result to days.

1

Got it

             d1=`date +%s -d $1`
             d2=`date +%s -d $2`
            ((diff_sec=d2-d1))
         echo - | awk -v SECS=$diff_sec '{printf "Number of days : %d",SECS/(60*60*24)}'

thanks..

1
  • 3
    If you're going to use (()) you might as well also use $() instead of backticks. Also, there's no need to pipe something into awk, just use awk ... 'BEGIN {printf ...}'. And you don't really need to use awk if it's just integer math: echo "Number of days: $(( ( d2 - d1 ) / ( 60 * 60 * 24 ) ))" Aug 2, 2010 at 11:16
0

Gnu date knows %j to display the day in year:

echo $(($(date -d 2010-06-01 +%j) - $(date -d 2010-05-15 +%j)))

crossing year-boundaries will give wrong results, but since you gave fixed dates ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.