Welcome to the Monastery PerlMonks

### find date given day weekday and a range of months

by kimlid2810 (Acolyte)
 on Aug 22, 2014 at 19:32 UTC Need Help??
kimlid2810 has asked for the wisdom of the Perl Monks concerning the following question:

greetings monks i have a slightly bizarre problem and i would be grateful if someone can offer some advice. i have 3 data. the day of a month eg: 28, the corresponding weekday eg: Thu, the year eg: 2014 and a range of three months eg: Aug, Sep, Oct like this:
```
my \$day = 28;
my \$weekday = 'Thu';
my \$year = '2014';
my @months = ('Aug', 'Sep', 'Oct');

Is there anyway i can figure out the exact date? Any module that would take this data and return something like:
```
my \$date = '2014-09-28';

Any algorithm known or any advice? Thank you :)

Replies are listed 'Best First'.
Re: find date given day weekday and a range of months
by kennethk (Abbot) on Aug 22, 2014 at 19:50 UTC
Not directly, because it's an under-determined problem. Under ordinary circumstances, days in February and days in March that share an ordinal value also share a day of the week. That said, you could perform the necessary search using the DateTime module, like:
```use DateTime;
for my \$mon (8, 9, 10) {
if (DateTime->new(year => 2014, month => 10, day => 28)->day_abbr(
+) eq 'Thu') {
print "Hit: \$mon\n";
}
}

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: find date given day weekday and a range of months
by poj (Prior) on Aug 22, 2014 at 19:47 UTC
```#!perl
use strict;
use Time::Piece;
my \$day = 28;
my \$weekday = 'Thu';
my \$year = '2014';
my @months = ('Aug', 'Sep', 'Oct');
for my \$mth (@months){
my \$t = Time::Piece->strptime("\$day-\$mth-\$year","%d-%b-%Y");
print \$t->ymd."\n" if \$t->wdayname eq \$weekday;
}
poj
Re: find date given day weekday and a range of months
by Anonymous Monk on Aug 22, 2014 at 23:32 UTC

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

Create A New User
Node Status?
node history
Node Type: perlquestion [id://1098370]
Front-paged by GotToBTru
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2017-11-23 13:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
In order to be able to say "I know Perl", you must have:

Results (335 votes). Check out past polls.

Notices?