Re: script with date operation??
On 5/13/06, Bruno Buys <email@example.com> wrote:
I am working on a script that needs to compute days. It needs to know
how many days have gone since some given date. for example, if I run it
today, it will need to know how many days have passed since, say, Feb,
02, 2006. And add the corresponding number to a variable.
I am looking at the man page for 'date', but it doesn't seem to help
much. Also, apt-cache search won't do better.
Is there any nice tool to operate dates at the console?
Since you're doing this in a script, it presumably doesn't need to be
done in a single line. I'd start with
date -d "Feb 2, 2006" +"%s"
to get the number of seconds since the epoch for midnight on the date
specified. Then I'd do
to get the same for the current time. Subtract those, and you get the
number of seconds since Feb 2, 2006. There are 86400 seconds in a
day. Divide, throw out the remainder, and you've got the number of
days since Feb 2, 2006.
If you *must* have a one-liner:
expr \( `date +"%s"` - `date -d "Feb 2, 2006" +"%s"` \) / 86400
Michael A. Marsh