Operating System - HP-UX
1833301 Members
2834 Online
110051 Solutions
New Discussion

Number of days between two dates?

 
SOLVED
Go to solution
Mary Rice
Frequent Advisor

Number of days between two dates?

Hi everybody,

Is there a way to calculate the exact number of days between two given dates? I've been scripting using the date command but there must be an easier way to do this!!!

Any suggestions, hints, or ideas are welcome.

Thank you, Mary
6 REPLIES 6
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Number of days between two dates?

Hi Mary,

Whenever I see 'dates' I perk up. Yes, that is rather easy if you use my date hammer (now where's your date nail?), caljd.sh.

JD1=$(caljd.sh 12 25 1999)
JD2=$(caljd.sh 03 31 2003)

NUMBER_OF_DAYS=$(( ${JD2} - ${JD1} ))

caljd.sh can take a calendar date and convert it to a Julian Day (~ the number of days since 4713BCE) and when supplied with a Julian Day it spits out the calendar date. Julian Days are used by astromers to make orbital calculations much easier.

Invoke caljd.sh -u for full usage.

Regards, Clay


If it ain't broke, I can fix that.
Craig Rants
Honored Contributor

Re: Number of days between two dates?

Who can beat Clay's caljd.sh, not I, but I will offer another solution. Use the epoch date, I do a lot with this, here are some variables that I define

CURRENT=`/usr/contrib/bin/perl -e "print time"`
NINETY='7776000'
SIXTY='5184000'
THIRTY='2592000'
NDAYS=`/usr/bin/expr $CURRENT - $NINETY`
SDAYS=`/usr/bin/expr $CURRENT - $SIXTY`
TDAYS=`/usr/bin/expr $CURRENT - $THIRTY`

You could modify these for you own purposes and come up with the same thing.

GL,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
SHABU KHAN
Trusted Contributor

Re: Number of days between two dates?

Mary,

Clay's caljd.sh is SUPER, also there was a perl script that was submitted in this forum a couple of weeks back which works pretty neat too, here is the link:

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0xe5d194f22a31d6118fff0090279cd0f9,00.html

It gives you the difference between two given dates in days, hours, minutes and seconds.

This script uses the Date-Calc perl module, if you don't already have it, download the tar ball from here:

http://search.cpan.org/search?dist=Date-Calc

Hope this helps !

-Shabu
Mary Rice
Frequent Advisor

Re: Number of days between two dates?

Thanks everyone and especially Clay. After your post I searched the Forum for "caljd.sh" and found many posts. I looked at the script and I still don't understand how it works but it does!!! I also was curious and did a Web search for "Julian Day" and learned a lot.

Thanks again, Mary
H.Merijn Brand (procura
Honored Contributor

Re: Number of days between two dates?

perl has the Date::Calc module that has the function 'Delta_Days':
$Dd = Delta_Days ($year1, $month1, $day1, $year2, $month2, $day2);
Enjoy, Have FUN! H.Merijn
A. Clay Stephenson
Acclaimed Contributor

Re: Number of days between two dates?

Hi Mary:

I'm glad that you liked it. I've used the C version of that algorithm for many years. I can't claim credit for Julian Days; the concept has been around for many, many years. The algorithms I use are adapted from those published in 'Sky and Telescope'. If you researched Julian Days then you know that those in caljd.sh are not true Julian Days. The real things are actually floating-point values and can represent fractional parts of days to any desired precision. The other difference is that Julian Days actually start at noon UTC so that midnight is JD.500000.
The ones in caljd.sh are truncated (integer) Julian Days and begin at midnite local time. These tend to be more useful in the computer world. You can specify the -U option to force the current date to be evaluated as UTC though again days begin at midnite.

My favorite part about all this is that the really tricky calculations are done in just two awk functions. CAL_JDATE converts MM DD YYYY into a Julian Day and JDATE_CAL does the reverse. The really neat thing is that "30 days hath September ..." ,including leap years, is done without a single if statement.
All the other stuff in the script is command line processing, usage, and stuff to read the /etc/acct/holidays file so that you can optionally skip holidays.


Regards, Clay
If it ain't broke, I can fix that.