Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate # of weeks between two dates in scripts 1

Status
Not open for further replies.

cruel

Programmer
Aug 6, 2001
131
Hi,
I need to calculate # of weeks between two dates in scripts. Appreciate ideas or reference.
 
Here's a brute force method using awk.

BEGIN {
m[1]=31;m[2]=28;m[3]=31;m[4]=30;
m[5]=31;m[6]=30;m[7]=31;m[8]=31;
m[9]=30;m[10]=31;m[11]=30;m[12]=31;
}
{
y1 = substr($1,1,4)+0
m1 = substr($1,5,2)+0
d1 = substr($1,7,2)+0
y2 = substr($2,1,4)+0
m2 = substr($2,5,2)+0
d2 = substr($2,7,2)+0
do {
d1 -= 7
if (d1 < 0) {
m1--
if (m1 <= 0) {
y1--
m1 = 12
}
if (m1==2) {
m[2] = 28
if (y1%4==0 && (y1%100!=0 || y1%400==0)) m[2]=29
}
d1 += m[m1]
}
#print d1 &quot; &quot; m1 &quot; &quot; y1
nw++
done = 0
if (y1<y2) done = 1
else if (y1==y2 && m1<m2) done = 1
else if (y1==y2 && m1==m2 && d1<=d2) done = 1
} while (!done)
print &quot;Number of weeks between &quot; $1 &quot; and &quot; $2 &quot; is &quot; nw
}

Put it in a file, weeks.awk say, and run it by entering

echo 20021016 20021001 | awk -f weeks.awk CaKiwi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top