[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.
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
- [coreutils] Days Between Dates FAQ Entry Added,
Bob Proulx <=