1753490 Members
5131 Online
108794 Solutions
New Discussion юеВ

Re: Simple calculation

 
SOLVED
Go to solution
Robert Comber
Advisor

Simple calculation

Hi experts,

If I have two dates like 11/30/04 and 1/10/05, is there a simple method to find the number of days between them? This ought to be easy but I can't think of a simple way.

TIA,
Bob
11 REPLIES 11
Patrick Wallek
Honored Contributor

Re: Simple calculation

Do a search through the forums for Clay Stephensons caljd.sh (or caljd.pl) program. That will allow you to do exactly what you require, plus a whole lot more.
Geoff Wild
Honored Contributor

Re: Simple calculation

Try this site -has some perl info - but if you wait - I'm sure Merijn will have a solution for you.... :)

Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Geoff Wild
Honored Contributor

Re: Simple calculation

Oops:

http://www.geocities.com/thestarman3/perl/perly2k.htm
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Simple calculation

#!/usr/bin/sh

DIFF=$(( $(caljd.sh -S "/" -C -c 1/10/05) - $(caljd.sh -S "/" -C -c 11/30/04) ))
echo "${DIFF} days"

Do a search for "caljd.sh" and you will probably find one or two hits in the Forums. Get Version 2.23 as it is the most recent.

Invoke as caljd.sh -u for full usage and examples.
If it ain't broke, I can fix that.
Robert-Jan Goossens
Honored Contributor

Re: Simple calculation

caljd.sh version 3.23

http://mirrors.develooper.com/hpux/#Contrib

Regards,
Robert-Jan
Robert Comber
Advisor

Re: Simple calculation

Hi guys,

Thanks. A Clay's solution worked but I hate to use a 1300 line script to do something so simple. Any other ideas?

TIA,
Bob
A. Clay Stephenson
Acclaimed Contributor

Re: Simple calculation

Well Bob, I always endeavor to write bloated, inefficient code. I probably should have used COBOL. Bear in mind that a significant chunk of this script is usage and examples plus extensive command-line argument parsing. If this bugs you, find the cal_jdate() function and just use it but then you will need to parse your dates (e.g. 11/30/04 into 11 30 2004 (04 is 4CE). The really neat thing about the cal_date() and the inverse jdate_cal() function is that the do all the "30 days hath September ..." and leap years without a single if.

On the other hand, if you can do better, have at it.
If it ain't broke, I can fix that.
Robert Comber
Advisor

Re: Simple calculation

Hi Clay,

Sorry, I didn't mean to make you mad. Please accept my apologies.

Thanks,
Bob
A. Clay Stephenson
Acclaimed Contributor

Re: Simple calculation

Not to worry. You didn't. Date calculations are far from trivial especially given that 365.2422 <> 365.25 and with the insane number of days in a given month -- oh, and even that number can vary with the year and whether or not its a century year evenly divisible by 400 ...
If it ain't broke, I can fix that.