How to count the number of Mondays, Tuesdays etc between two given dates ? (519 Views)
Reply
Frequent Advisor
Pankaj Yadav_1
Posts: 54
Registered: ‎08-03-2006
Message 1 of 12 (519 Views)
Accepted Solution

How to count the number of Mondays, Tuesdays etc between two given dates ?

Dates are in the form yyyy-mm-dd .
Please tell me .
Honored Contributor
RAC_1
Posts: 5,920
Registered: ‎03-21-2002
Message 2 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

There is tool (a script) by AC Clay. It is called date hammer. I don't know how that will help, but you may have a look at it.

Search forums for caljd.sh
There is no substitute to HARDWORK
Honored Contributor
Robert-Jan Goossens_1
Posts: 1,813
Registered: ‎04-04-2000
Message 3 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

Hi,

search inside this thread for the word caljd.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=123030

latest version of this script can be downloaded from.

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

Regards,
Robert-Jan
Frequent Advisor
Pankaj Yadav_1
Posts: 54
Registered: ‎08-03-2006
Message 4 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

i forgot to tell you tht the language is Perl.
Honored Contributor
Robert-Jan Goossens_1
Posts: 1,813
Registered: ‎04-04-2000
Message 5 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

perl version of the same caljd script.

http://mirrors.develooper.com/hpux/caljd-2.2.pl

regards,
Robert-Jan
Frequent Advisor
Pankaj Yadav_1
Posts: 54
Registered: ‎08-03-2006
Message 6 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

The script is large.
can i have a small script which I can insert into my perl cgi program ?
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 7 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

Hi,
once you have the script:
1. strip out the '-' out of the date
2. convert the start date to Julian with '-y' option.
3. Same for end date
4. run a loop while start date < end date
5. use -w to get the weekday
6. case statement to add 1 to the Mon,Tue,Wed... array
7. end of loop
8. Print out your array

Bourne version of this attached
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 8 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

Hi Pankaj:

One way is to use Perl's Date::Calc module. You can verify your dates (if necessary) with its 'check_date' function and determine the difference between a set of dates with its 'Delta_Days' function.

If you want to roll-you-own, you can use the Time::Local module; regular expressions or unpack() to parse your dates; convert to epoch secnods and determine the delta value in days.

Once you have the number of days, inclusive or exclusive of the range points, a "div 7" will give you the number of Mondays, Tuesdays or whatever represented the day associated with the beginning date.

Regards!

...JRF...
Acclaimed Contributor
A. Clay Stephenson
Posts: 17,825
Registered: ‎07-16-1998
Message 9 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

While the caljd.pl script is large the two subroutines that are the heart of the script are just a few lines long each. Just yank out the Cal_Jdate subroutine pass it month, day, and year and it will return a true Julian Day. Jade_Cal does the reverse and there is also a Wkday subroutine. Pass it a Julian Day and it will return 0 for Sunday, 6 for Saturday.

The reason that this script is so large is primarily argument parsing and usage and examples but also providing NLS support adds to the complexity but in any event the subroutines that you need are very small.
If it ain't broke, I can fix that.
Honored Contributor
Peter Godron
Posts: 4,470
Registered: ‎02-13-2002
Message 10 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

Pankaj,
log of my earlier post:
$ ./x.sh "2006-09-14" "2006-09-22"
Start Julian: 2453993
End Julian: 2454001
Mondays : 1
Tuesdays : 1
Wednesdays : 1
Thursdays : 2
Fridays : 1
Saturdays : 1
Sundays : 1
Honored Contributor
Hein van den Heuvel
Posts: 6,588
Registered: ‎05-19-2003
Message 11 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

I had something like that floating around.
Modified it to do the brute-force count, like Peter did.

It should basically be a simple divide by 7 of end day minus begin day but taking the begin and end day-of-the week into account made my head hurt.

Note, there is one gotcha if you play with timelocal local. You can get a surpise 3600 seconds difference on the right/wrong days.
In 2006 in the EDT zone 0h0 4/2 - 0h0 4-3 = 82800 seconds 0.958333333333333 days, not 86400 and 1.
Rounding takes care of that as needed.

Brute-force perl:

use Time::Local;
sub mytime {
my ($y,$m,$d) = split /-/,$_[0];
return timelocal(0,0,0,$d,$m-1,$y);
}
$beg = mytime(shift @ARGV);
$end = mytime(shift @ARGV);
@nam = qw/Sun Mon Tue Wed Thu Fri Sat/;
for $i (0..6) { $weeks[$i]=0 }
# barely tolerant for daylight saving jumps
for ($i=$beg; $i < $end; $i += 86400) {
$weeks[(localtime($i))[6]]++;
}
for $i (0..6) {
print "$nam[$i] $weeks[$i]\n";
}


Cheers,
Hein.
Frequent Advisor
Pankaj Yadav_1
Posts: 54
Registered: ‎08-03-2006
Message 12 of 12 (519 Views)

Re: How to count the number of Mondays, Tuesdays etc between two given dates ?

I divided the no. of days by 7 and that gave me the answer.
Also I took care of the remainder by the modulus(%) operator to calculate the exact no. of Mondays, Tuesdays etc.

I have given you all points.
Thanks a lot to all of you.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.