Hello,

For those interested by an implementation of this technique,

a shell script is available at
http://scriptutils.svn.sourceforge.net/viewvc/scriptutils/sandbox/datediff.sh?revision=58


Example: duration until Christmas
     $ ./datediff.sh -p 2010-12-25
     6 months 2 days 20 hours 13 minutes 34.0032 seconds


Sebastien



On Tue, Jun 22, 2010 at 12:57 AM, Bob Proulx <[email protected]> wrote:

> I have added a tip to the date faq entry.  It hasn't been an faq but
> but seemed useful just the same.
>
>
> http://www.gnu.org/software/coreutils/faq/#The-date-command-is-not-working-right_002e
>
>  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.
>
>       date1="2008-01-01"
>       date2="2010-06-21"
>       date1seconds=$(date -d "$date1 12:00" +%s)
>       date2seconds=$(date -d "$date2 12:00" +%s)
>       totalseconds=$(( $date2seconds - $date1seconds ))
>       secondsperday=86400
>       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 ))
>
> Bob
>

Reply via email to