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

Re: Command-line-interface (CLI) calculator to work out the difference between 2 dates



On 08/24/08 11:32, Rick Pasotto wrote:
On Sun, Aug 24, 2008 at 11:25:31AM -0500, Ron Johnson wrote:
On 08/24/08 10:54, Edward J. Shornock wrote:
[snip]
While I'm sure someone else will provide a much better way, I've used something like

$ echo $(date  -d 20080824 +%j) - $(date  -d 20080724 +%j) | bc
31
That a good idea. I never knew date(1) could do that. The problem, though, is that it doesn't span years.

$ echo $(date -d 20090824 +%j) - $(date -d 20080724 +%j) | bc
30

Use delta from epoch, instead.
$ echo $[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)]/86400|bc
396

Why pipe it to bc? Keep it in the shell:

$ echo $[$[$(date -d 20090824 +%s) - $(date -d 20080724 +%s)] / 86400]
396

I was thinking that integer division might result in the occasional rounding error. But now that I think about it, that wouldn't matter, since bc does integer math unless you pass it "-l".

One stylistic reason for piping to bc is that some people think that bash's $[] syntax gets too hard to read if you nest it too much.

--
Ron Johnson, Jr.
Jefferson LA  USA

"Do not bite at the bait of pleasure till you know there is no
hook beneath it."  -- Thomas Jefferson


Reply to: