1834535 Members
3398 Online
110069 Solutions
New Discussion

Re: Days between 2 dates

 
SOLVED
Go to solution
John Chaff
Advisor

Days between 2 dates

Hi:

A colleague told me about this site. I have two dates, 11/26/99 and 03/05/02, for example. It there a UNIX utility to determine the number of days between these two dates? This should be so simple.

Thanks in advance for your help,
John Chaff

10 REPLIES 10
Rajeev  Shukla
Honored Contributor

Re: Days between 2 dates

There is no utility as such in unix to do that but i am sure its not difficult. I had posted similar programs in C before, let me see if i can find them, but in the mean time i can tell you the logic in C.
Convery both the dates to sec from 1970 using time() function and then substract them and divide them by 3600 you'll get hours and then further divide by 24 to convery into days.
Ramkumar Devanathan
Honored Contributor

Re: Days between 2 dates

John,

Try Clay Stephenson's caljd perl script - it is easy to use. usage details are provided within.

https://www.beepz.com/personal/merijn/caljd-2.2.pl

- ramd.
HPE Software Rocks!
Steven E. Protter
Exalted Contributor

Re: Days between 2 dates

There is a wonderful utility that does this written by A. Clay Stephenson.

caljd.sh
caljd.pl

http://www.hpux.ws/merijn/caldj-2.2.sh

http://www.hpux.ws/merijn/caljd-2.2.pl

These will do the trick. Well documented, easy to use.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Days between 2 dates

This here is right easy.

#!/usr/bin/sh
DT1="11/26/99"
DT2="03/05/02"

DIFF=$(( $(caljd.sh -c -C -S "/" ${D2}) - $(caljd.sh -c -C -S "/" ${D1}) ))
echo "Difference = ${DIFF} days."

You will need a recent version of caljd.sh because I added the -C option to interpret 2-digit years as recent years rather than 1st-century CE dates. I do assume that you meant 1999CE rather than 99CE. 99CE would give ctime() functions fits. Invoke as caljd.sh -u for full usage but this should be exactly what you want.


Attached is caljd.sh, Version 2.22.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Days between 2 dates

And here is the Perl version, if you prefer. The arguments are exactly the same.

Attached is caljd.pl, Version 2.22p.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Days between 2 dates

Ooops, John, I'm an idiot.

THIS HERE:
DIFF=$(( $(caljd.sh -c -C -S "/" ${D2}) - $(caljd.sh -c -C -S "/" ${D1}) ))
SHOULD BE:
DIFF=$(( $(caljd.sh -c -C -S "/" ${DT2}) - $(caljd.sh -c -C -S "/" ${DT1}) ))

No points please, Clay.
If it ain't broke, I can fix that.
John Chaff
Advisor

Re: Days between 2 dates

Thanks everybody. That is a great script. I have one more question. I would like to print the usage messages but nothing goes to the printer when I type "caljd.sh -u|lp" and also if I use "caljd.sh -u|more" it scrolls off the screen without pausing. I'm confused.

Thanks,
John Chaff

Robert-Jan Goossens
Honored Contributor

Re: Days between 2 dates

Hi John,

# cd /tmp
# nohup caljd.sh -u
Sending output to nohup.out
# lp nohup.out

Regards,
Robert-Jan.
A. Clay Stephenson
Acclaimed Contributor

Re: Days between 2 dates

Like good UNIX scripts, usage and error messages go to stderr rather than stdout and that is your problem. Stdout should only be used for output.


It's trivially easy, just redirect stderr (file descriptor 2) onto stdout (fdes 1), viz:
caljd.sh -u 2>&1 | lp
OR
caljd.sh -u 2>&1 | more

That should fix you, Clay.

If it ain't broke, I can fix that.
John Chaff
Advisor

Re: Days between 2 dates

Thanks everybody for the quick and accurate solutions.