[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[coreutils] Days Between Dates FAQ Entry Added

From: Bob Proulx
Subject: [coreutils] Days Between Dates FAQ Entry Added
Date: Mon, 21 Jun 2010 16:57:32 -0600
User-agent: Mutt/1.5.18 (2008-05-17)

I have added a tip to the date faq entry.  It hasn't been an faq but
but seemed useful just the same.

  Tip: GNU date itself doesn't include any direct support for finding
  days between dates.  But it can be used as part of a script to find
  this information.  The technique is to convert each date to an
  integer value such as a Julian Day Number or seconds since the Unix
  epoch 1970-01-01 00:00:00 UTC and then do take the difference and
  then convert the seconds to days.  Use Unix seconds is very
  convenient due to the %s format.  Here is an example.

       date1seconds=$(date -d "$date1 12:00" +%s)
       date2seconds=$(date -d "$date2 12:00" +%s)
       totalseconds=$(( $date2seconds - $date1seconds ))
       days=$(( $totalseconds / $secondsperday ))
       echo "There were $days days between $date1 and $date2"

  And of course that can be considerably compacted by applying the
  constructs inline but this is not as illustrative and so was
  expanded out in the above example.  Here is a compact version.

       $ echo Days between dates: $(( ( $(date -d "2010-06-21 12:00" +%s) - 
$(date -d "2008-01-01 12:00" +%s) ) / 86400 ))


reply via email to

[Prev in Thread] Current Thread [Next in Thread]