TIMTOWTDI! :-)

my $day = 28; my $weekday = 'Thu'; my $year = '2014'; my @months = ('Aug', 'Sep', 'Oct'); my @mons = qw/ Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec /; my %mons = map {$mons[$_]=>$_+1} 0..$#mons; @months = map {$mons{$_}} @months; # convert @months to numerical my @dow = qw/ Sun Mon Tue Wed Thu Fri Sat /; my %dow = map {$dow[$_]=>$_} 0..$#dow; $weekday = $dow{$weekday}; # convert $weekday to numerical use POSIX qw/mktime/; # core use Time::Piece (); # core since v5.10 use DateTime (); # CPAN use Date::Calc qw/Day_of_Week/; # CPAN use Date::Manip qw/Date_DayOfWeek/; # CPAN for my $mon (@months) { my $date = sprintf "%04d-%02d-%02d", $year, $mon, $day; print " mktime: $date\n" # 0 = Sunday if (localtime mktime(0,0,0,$day,$mon-1,$year-1900))[6] == $weekday; print "Time::Piece: $date\n" # 0 = Sunday if Time::Piece->strptime("$day-$mon-$year","%d-%m-%Y") ->day_of_week == $weekday; print " DateTime: $date\n" # 1 = Monday, 7 = Sunday if DateTime->new(year=>$year, month=>$mon, day=>$day) ->dow%7 == $weekday; print " Date::Calc: $date\n" # 1 = Monday, 7 = Sunday if Day_of_Week($year,$mon,$day)%7 == $weekday; print "Date::Manip: $date\n" # 1 = Monday, 7 = Sunday if Date_DayOfWeek($mon,$day,$year)%7 == $weekday; } __END__ # Output: mktime: 2014-08-28 Time::Piece: 2014-08-28 DateTime: 2014-08-28 Date::Calc: 2014-08-28 Date::Manip: 2014-08-28

In reply to Re: find date given day weekday and a range of months by Anonymous Monk
in thread find date given day weekday and a range of months by kimlid2810

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.