1846552 Members
2218 Online
110256 Solutions
New Discussion

Re: Scripting Help

 
SOLVED
Go to solution
Smirjit Singh
Advisor

Scripting Help

Hi,

I need help to calculate date difference.

Suppose if anyone pass two date then it should return actual day difference. It should calculate leap year also.

Like:
datediff.sh 25/01/2001 20/03/2001
It should return 55 days.

Pls help me.

Knowledge is only most valuable things which can't buy.
4 REPLIES 4
Bill Hassell
Honored Contributor
Solution

Re: Scripting Help

Here's a simple program from David Totsch that can help...dtime compiles with the built-in compiler:

ftp://contrib:9unsupp8@hprc.external.hp.com/sysadmin/programs/


Bill Hassell, sysadmin
linuxfan
Honored Contributor

Re: Scripting Help

Hi Sandip,

Using the caljd.sh script, this is a piece of cake. Believe me caljd.sh is pretty powerful dealing with dates, calculating different kind of dates/days of week. takes leap years in to consideration as well.

You could do something like,

/Begin/

#!/usr/bin/ksh

PATH=/usr/bin:/usr/local/bin

arg1=`echo $1 | sed 's/\// /g'`
arg2=`echo $2 | sed 's/\// /g'`

temp1=`caljd.sh $arg1`
temp2=`caljd.sh $arg2`

# temp2=`caljd.sh 03 20 2001`
# temp1=`caljd.sh 01 25 2001`

echo "$temp1-$temp2" | bc

/End/

This script will calculate the difference between the two arguments given to this program.

Remember to give the dates in the format mm/dd/yy

-HTH
Ramesh
They think they know but don't. At least I know I don't know - Socrates
A. Clay Stephenson
Acclaimed Contributor

Re: Scripting Help

Hi:

Sine I posted caljd.sh, I have found a small but stupid bug on my part. I missed one integer divide and it evaluated to a floating point divide. This could lead to small errors but this version of caljd.sh corrects that.

Clay
If it ain't broke, I can fix that.
Smirjit Singh
Advisor

Re: Scripting Help

Thanks to all to their wonderful tips.
Knowledge is only most valuable things which can't buy.