Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Finding the Day of Week in Windows

by dru145 (Friar)
on Jan 06, 2003 at 21:30 UTC ( [id://224753]=perlquestion: print w/replies, xml ) Need Help??

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

Can someone please tell me how I find the abbreviated day of the week (ie: Mon) using ActiveState Perl. I can only get it by number:
use warnings; use strict; use Time::localtime; my $tm = localtime(); my $day = $tm->wday; print "Today is $day\n"
Thanks,
Dru
Another satisfied monk.

Replies are listed 'Best First'.
Re: Finding the Day of Week in Windows
by Mr. Muskrat (Canon) on Jan 06, 2003 at 21:42 UTC

    I can think of twothree quick ways:

    my $today = localtime; my ($day) = $today =~ m/^(\w{3})/;

    my $dow = qw(Sun Mon Tue Wed Thu Fri Sat)[(localtime())[6]];
    and
    use POSIX qw(strftime); my $dow = strftime "%a", localtime;

    Update: Added third method.

Re: Finding the Day of Week in Windows
by fredopalus (Friar) on Jan 07, 2003 at 00:01 UTC
    The reason it was printing in numbers is because you weren't printing it in scalar context. Try:
    $time = scalar localtime; $day = substr($time, 0,3); print $day;

    If it's printed without scalar context, it gives a list of values in the format:
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)

    See the documentation page on localtime() for more info.
Re: Finding the Day of Week in Windows
by BigLug (Chaplain) on Jan 07, 2003 at 03:21 UTC
    I know its not what you asked for .. you're looking at localtime methods, but I'll contribute the following just for interests sake:

    This method will take a day, month and year and tell you what day of the week it was/is/will be.

    I know this doesn't make sense to the human eye, but believe me, its an official algorithm for calculating the day of the week. I've just taken the algorithm and turned it into a perl version. If you want more info, search google for something like http://www.google.com/search?q=calculate+the+day+of+the+week

    sub dow { ($year, $month, $day) = @_; $year-- if $month < 3; return ($year+int($year/4)-int($year/100)+int($year/400)+((0,3,2,5 +,0,3,5,1,4,6,2,4)[$month-1])+$day) % 7; } print (('Sun','Mon','Tues','Wed','Thu','Fri','Sat')[dow(2002,07,12)]);
    As with localtime, the algorithm returns a number that relates to the day of the week. By using the return as the index to an anonymous array, we get the actual day.
      This algorithm is perfect for programs in other languages, but not in Perl... Why use something like this at all when there are so many builtins that do the job???
        Name one that doesn't require any modules. And I mean any modules. Including POSIX. Sure, I know that they all come with perl but why open yet-another file when if all you want from it is to compute the day of the week for a particular date?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://224753]
Approved by Mr. Muskrat
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (None)
    As of 2024-04-25 00:45 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      No recent polls found