Operating System - HP-UX
1838727 Members
7176 Online
110129 Solutions
New Discussion

Re: How to count the days between two dates.

 
SOLVED
Go to solution
Steve Start
Advisor

How to count the days between two dates.

Hi guys,

Is there an HP-UX command that will count the number of days between two dates? I've searched the man pages and can't find anything close. This should be so simple but I can't find anything that will do this.

Thanks,
Steve
19 REPLIES 19
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: How to count the days between two dates.

Well there ain't no HP-UX command that will do this but I just might know something that might do the trick for you; it's called caljd.sh. Invoke as caljd.sh -u for full usage.

#!/usr/bin/sh

D1="01 01 2004"
D2="03 24 2004"
typeset -i10 DIFF=$(( ${caljd.sh ${D2}) - $(caljd.sh ${D1}) ))
echo "Diff = ${DIFF} days."

Here's caljd.sh:
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to count the days between two dates.

Oops,

typeset -i10 DIFF=$(( ${caljd.sh ${D2}) - $(caljd.sh ${D1}) ))

should be:

typeset -i10 DIFF=$(( $(caljd.sh ${D2}) - $(caljd.sh ${D1}) ))

Stupid me, I swaped '{' for '('.
If it ain't broke, I can fix that.
Steve Start
Advisor

Re: How to count the days between two dates.

Hi A. Clay,

I tried your solution and it gives me this error:

datediff[11]: 11 24 4713 - 11 24 4713 : Syntax error

Any ideas?

Thanks,
Steve
Patrick Wallek
Honored Contributor

Re: How to count the days between two dates.

It looks like you didn't download and install caljd.sh. If you did, did you make it executable?
A. Clay Stephenson
Acclaimed Contributor

Re: How to count the days between two dates.

Well Steve, my best guess is that you "improved" my script from this:

D1="01 01 2004"
D2="03 24 2004"
typeset -i10 DIFF=$(( $(caljd.sh ${D2}) - $(caljd.sh ${D1}) ))
echo "Diff = ${DIFF} days."

to something like this:

D1="01/01/2004"
D2="03/24/2004"
typeset -i10 DIFF=$(( $(caljd.sh ${D2}) - $(caljd.sh ${D1}) ))
echo "Diff = ${DIFF} days."

Caljd.sh expect 3 arguments for a Gregorian Date and you are supplying only 1.

You could change it to something like this:
D1="01/01/2004"
D2="03/24/2004"
typeset -i10 DIFF=$(( $(caljd.sh -S "/" -c ${D2}) - $(caljd.sh -S "/" -c ${D1}) ))
echo "Diff = ${DIFF} days."

and that should work; the -c says this is a Gregorian calendar date.

Again, invoke as caljd.sh and you will get many examples.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: How to count the days between two dates.

Ooops, again, invoke as caljd.sh -u for full usage and many examples. You can also search the forums for more solutions.

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

Re: How to count the days between two dates.

Hi guys and thanks for your patience. Patrick, I did install the caljd.sh program and make it executable. A. Clay, my dates are 07/15/99 and 03/15/04. I no longer get an error but the result doesn't make sense. The result is -34820!! There must be a bug in the script somewhere.

Thanks,
Steve
A. Clay Stephenson
Acclaimed Contributor

Re: How to count the days between two dates.

Okay Steve,

You're welcome for your thanks but I am now officially out of patience. I forgot to mention that after you invoke as caljd.sh -u that is necessary to actually read the usage messages and examples.

Actually your result makes perfect sense;
the difference between 07/15/99 CE (or AD) and 03/15/04 CE is -34820 days! You probably meant 07/15/1999 but that dumb computer had no way of knowing that. Add an additional "-C" argument to each invocation of caljd.sh and that should fix you ---- and yes, this too, is explained in those silly, useless usage messages. Maybe I need to create a man page but I bet you don't bother with them either.

I feel better now,
Clay


If it ain't broke, I can fix that.
Steve Start
Advisor

Re: How to count the days between two dates.

Hi A. Clay,

Sorry I didn't mean to make you mad. The last change using the -C option worked and the result was 1705 days. I'll have to get out the calendar and count the days but that looks right.

Thanks for all your help and next time I will closely read all the examples.

Thanks,
Steve
Hein van den Heuvel
Honored Contributor

Re: How to count the days between two dates.

Steve wrote:
"There must be a bug in the script somewhere."

Lemme see,
On the one hand there is a script (program/application/tool, fill in the appropriate condition...) heavily recommended, heavily used, by tons of folks, tons of times.
On the other hand there is this user, who has a problem to be solved, who is trying to use to use said script/tool/application for a very first time, but that fails.

Hmmm, where might the problem be? In the tool or in its user/usage?

Nothing directed to you personnaly Steve, but I see this a lot and fail to understand how in such situation one can even consider that it would be anything other then a usage problem!

cheers!
Hein.



Steven E. Protter
Exalted Contributor

Re: How to count the days between two dates.

Let me stand behind A. Clay's script. I use it in production on 5 machines, ported it to Linux and now use it on six others.

There are not any bugs in the script.

If you wish to engage a programmer to do this for you fine. I'm available.

A. Clay has made a tremendous effort and deserves point assignment.

Please do so.

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
Jeff Schussele
Honored Contributor

Re: How to count the days between two dates.

And just what might your rate be SEP?

I'd really like to think you know you're one fine step away from spam in my opinion. But I'm having doubts.

I sincerely wish you'd knock that off.

This is a forum - period - not a commercial publication. And if it was, I'd urge Dan to charge you an advertising fee. At least a % of your take.

My 2 cents,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Steven E. Protter
Exalted Contributor

Re: How to count the days between two dates.

If my joke was misunderstood, I apologize.

A. Clay has provided the programs, and done the shell scripting for you. His programs are not broken.

Whooooooooooooooosh

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

Re: How to count the days between two dates.

You boys need to be learning how to lighten up. I see right now that your Mama's didn't whup you nearly enough. As for them there points, there ain't no use worrying about them none neither. I find that if you do a good enough job, them points takes care of their ownselves.

Happy Trails and all that,
Clay
If it ain't broke, I can fix that.
Jeff Schussele
Honored Contributor

Re: How to count the days between two dates.

Sorry Clay.
I didn't start this, but I will finish it.
And believe me, I do have the welts.
I wonder about others....

Best Rgds,
Jeff
PERSEVERANCE -- Remember, whatever does not kill you only makes you stronger!
Michael Schulte zur Sur
Honored Contributor

Re: How to count the days between two dates.

Sorry Jeff,

you are going overboard on this.
Even is you do not get SEP's joke or do not want to get it, there is no way, that you can classify SEP's post as spam. Please, give me a break.

ACS has achieved everything there is to. He is is the top of the crop, he is olympian, so he acts according to the bible verse "Sow much and you will reap much". ;-)

As he said, lighten up,

Michael
Daryl Much
Frequent Advisor

Re: How to count the days between two dates.

I use perl and the Date::Manip module. It is *extremely* flexible in date/time formats, etc.

Chuck Davis
Steve Start
Advisor

Re: How to count the days between two dates.

Hi guys,

Sorry about being late with my points assignments. I have also been properly chastised for not reading the examples. I am really amazed at what this script can do. I also searched and found the perl version of this wonderful script. Until reading about this subject, I never really thought about how hard this really is. I still don't quite know where 11 24 4713 came from when I didn't correctly use the command but I have used the script enough to know that it even handles leap years with no problems.

Thanks for everyone's input.

Steve
A. Clay Stephenson
Acclaimed Contributor

Re: How to count the days between two dates.

Actually Steve, you were right. There is a bug. Your question about "11 24 4713" made be realize that I was chopping off the leading minus because that should be -4713 (BCE). I rather doubt that that bug will cause any problems in any uses that any of us have for caljd.sh but nevertheless the bug exists. 11 24 -4713 is the Gregorian calendar date for Julian Day 0. 01 01 -4712 is the equivalent in the Julian calendar. Of course, both of these dates are proleptic dates (i.e. dates projected back before the calendars came into being.) The Gregorian calendar assumes a year is 365.2420 days long whereas the Julian calendar assumes the year is 365.2500 days. The actual value is 365.2422 but that is only an approximation because the rotational speed of the earth is not constant. If you don't think this stuff matters, do a UNIX cal 9 1752 and see if something looks a little strange. This was the month that the Gregorian calendar was adopted by Great Britain and her colonies.

Anyway, here is the corrected version; caljd.sh Version 2.23; the Perl version 2.22p is fine.

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