Operating System - Linux
1748246 Members
3165 Online
108760 Solutions
New Discussion юеВ

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

 
SOLVED
Go to solution
Pankaj Yadav_1
Frequent Advisor

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

Dates are in the form yyyy-mm-dd .
Please tell me .
11 REPLIES 11
RAC_1
Honored Contributor

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
Robert-Jan Goossens_1
Honored Contributor

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
Pankaj Yadav_1
Frequent Advisor

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

i forgot to tell you tht the language is Perl.
Robert-Jan Goossens_1
Honored Contributor

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
Pankaj Yadav_1
Frequent Advisor

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 ?
Peter Godron
Honored Contributor

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
James R. Ferguson
Acclaimed Contributor
Solution

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...
A. Clay Stephenson
Acclaimed Contributor

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.
Peter Godron
Honored Contributor

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