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

I know I can get the weekday(Monday,...) for today using localtime(6).

My question is,
how do i find out what weekday does any certain date(ex. May 21,1981) fall on????

Replies are listed 'Best First'.
Re: DATE =TO= DAY OF WEEK
by plaid (Chaplain) on May 05, 2000 at 03:38 UTC
    Either of the above mentioned ways will work.. however, I would recommend using the Date::Calc method for one main reason: It's not dependant on the number of bits that the underlying machine uses to store its dates in. Both will work the same way for dates between the years 1901 and 2038 (roughly), but beyond that range, localtime will return incorrect values on 32 bit systems (which all x86 machines still are). I don't know if this will be an issue for you, but it's a good thing to know.

    On a minor issue, you might also prefer Date::Calc because it lets you specify dates in a more intuitive way than Time::Local, which makes you subtract 1 from the month, since it's 0 based, and subtract 1900 from the year (I believe).

Re: DATE =TO= DAY OF WEEK
by perlmonkey (Hermit) on May 05, 2000 at 03:13 UTC
    easist way (in my opinion) is to use Date::Calc
    use Date::Calc qw(Parse_Date Day_of_Week Day_of_Week_to_Text); $day = Day_of_Week(Parse_Date("May 21,1981")); print Day_of_Week_to_Text($day);
Re: DATE =TO= DAY OF WEEK
by btrott (Parson) on May 05, 2000 at 03:22 UTC
    Or you could use Time::Local to turn a date spec into an epoch time (as returned by time), then give that to localtime--from the docs:
    $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
    So, for your case:
    use Time::Local; my $time = timelocal(0, 0, 0, 21, 4, 1981); my $wday = (localtime $time)[6];
Re: DATE =TO= DAY OF WEEK
by httptech (Chaplain) on May 05, 2000 at 04:32 UTC
    use Date::Manip; my $day = UnixDate("May 21,1981", "%A");
Re: DATE =TO= DAY OF WEEK
by BBQ (Curate) on May 05, 2000 at 08:12 UTC
    Try this...
    sub TextDate { my ($time) = $_[0]; my @days = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat'); my @months = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep +','Oct','Nov','Dec'); $time = time() if ! $time; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localti +me($time); if ($hour < 10) { $hour = "0$hour"; } if ($min < 10) { $min = "0$min"; } if ($sec < 10) { $sec = "0$sec"; } $year += 1900; return("$days[$wday] $mday/$months[$mon]/$year \@ $hour\:$min"); }


    #!/home/bbq/bin/perl
    # Trust no1!
Re: DATE =TO= DAY OF WEEK
by chromatic (Archbishop) on May 05, 2000 at 06:32 UTC
    Don't forget that localtime can also take an expression in time() format. You could figure out the time of your date, pass it to localtime, and then grab what's in slot 6.

    I wouldn't do it that way, but:

    my @date = localtime(time); print $date[6];
    How about that, Thursday is the 4th day of the week!