fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

hi How can I find all Thursdays for a given year? Thanks in advance.

Replies are listed 'Best First'.
Re: find weekdays for a year
by zek152 (Pilgrim) on Jul 19, 2011 at 13:43 UTC

    For future reference you should show the things that you have tried. We aren't a code-writing service.

    That being said you should look at Date::Simple. What you want to do is find the first thursday of a year. Date::Simple uses Sunday as the 0th day of the week so Thurs is the 4th. I used Date::Calc because it was on my computer already.

    use Date::Calc qw(:all); $count = 0; #insert your own year. $year = "2011"; #set $date to the first day of the year @date = ($year,1,1); print Date_to_Text(@date) . "\n"; #find the next thursday if(Day_of_Week(@date) > 4) { @date= Add_Delta_Days(@date,7-Day_of_Week(@date)+4); } else { @date=Add_Delta_Days(@date,4-Day_of_Week(@date)); } #now you have the first thursday. Now just count how many #until you reach a new year while(@date[0] eq $year) { $count++; @date=Add_Delta_Days(@date,7); } print "$count thursdays in $year\n";

    You should probably verify that the results are correct. They seem to make sense to me but that doesn't mean much. Hope this helps.

      found this...thanks
      1 use strict; 2 use warnings; 3 4 use DateTime::Event::Recurrence; 5 6 7 my $dt1 = new DateTime( year => 2011 ); 8 my $dt2 = new DateTime( year => 2012 ); 9 my $der = DateTime::Event::Recurrence->weekly(days=>4); # + Thursday is #4 10 print $_->ymd, ' ', ,$/ for 11 $der->as_list( start => $dt1, end => $dt2 );
Re: find weekdays for a year
by Anonymous Monk on Jul 19, 2011 at 13:33 UTC
Re: find weekdays for a year
by osbosb (Monk) on Jul 19, 2011 at 13:29 UTC
    I'm sure if you looked into any of the numerous modules available on cpan you'd find what you're looking for. Also take a look at -> How (Not) To Ask A Question
Re: find weekdays for a year
by ambrus (Abbot) on Jul 19, 2011 at 18:32 UTC