Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Day of week for an arbitary date using core modules

by merlyn (Sage)
on Feb 21, 2002 at 14:12 UTC ( #146755=snippet: print w/ replies, xml ) Need Help??

Description: From an answer I just posted in alt.perl. How to get the day of the week for a given date, both as a number, and as an abbreviation. Works for 1970 to 2038.
    use Time::Local;
    my ($year, $month, $day) = qw(2002 02 21);
    my $gmtime = timegm(0,0,0,$day,$month-1,$year-1900);
    my @gmtime = gmtime($gmtime); # convert back
    print "day of week is $gmtime[6]\n"; # sunday = 0, saturday = 6
    my ($day_of_week) = gmtime($gmtime) =~ /^(\S+)/; # fetch first wor
    print "day of week is $day_of_week\n"; # "Sun" .. "Sat"
Comment on Day of week for an arbitary date using core modules
Download Code
Re: Day of week for an arbitary date using core modules
by blakem (Monsignor) on Feb 21, 2002 at 20:20 UTC
    The new Time::Piece interface works very well for this... (though its a "future core module" rather than a current one)
    #!/usr/bin/perl -wT use strict; use Time::Piece; my ($year, $month, $day) = qw(2002 02 21); my $t = Time::Piece->strptime("$year/$month/$day", "%Y/%m/%d"); print "day of week is " . $t->day_of_week . "\n"; print "day of week is " . $t->day . "\n";


      Last I read, Time::Piece wasn't going to be added to the core. Don't know why, but it's a shame because it's a fine module.


      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

Re: &bull;Day of week for an arbitary date using core modules
by YuckFoo (Abbot) on Feb 22, 2002 at 17:29 UTC
    I found this jewel in Greg Hewgill's implementation of the cal command on the Perl Power Tools page:


    # DOW = ([23m/9] + d + 4 + z + [z/4] - [z/100] + [z/400] - 2 (if m>=3 +)) mod 7 # # y is the year. # m is the month. # d is the day. # z = y - 1 (if m < 3) # = y (if m >= 3) # A mod B means take the reminder of A / B. # # The source: Journal on Recreational Mathematics, Vol. 22(4), pages +280-282, 1990. # The authors: Michael Keith and Tom Craver. # # The formula can be implemented by the following C function: # # int dayofweek(int y,m,d) # { # return((23*m/9+d+4+(m<3?y--:y-2)+y/4-y/100+y/400)%7); # } sub dow { my ($y, $m, $d) = @_; $y-- if $m < 3; $d += 11 if $y < 1752 || $y == 1752 && $m < 9; if ($y >= 1752) { return (int(23*$m/9)+$d+4+($m<3?$y+1:$y-2)+int($y/4)-int($y/100)+i +nt($y/400))%7; } else { return (int(23*$m/9)+$d+5+($m<3?$y+1:$y-2)+int($y/4))%7; } }
Re: •Day of week for an arbitary date using core modules
by meonkeys (Chaplain) on Apr 28, 2002 at 05:51 UTC
    Off topic, but what would you (merlyn, whomever) do if you didn't have to use core modules? Just curious what y'all prefer for date parsing and formatting. I'd do this...
    #!/usr/bin/perl -Tw use strict; use Date::Format; use Date::Parse; my $time = str2time('2002-02-21'); my $day_of_wk = time2str("%w", $time); print "day of week is $day_of_wk\n"; $day_of_wk = time2str("%A", $time); print "day of week is $day_of_wk\n";
    "A Jedi uses the Force for knowledge and defense, never for attack."

Back to Snippets Section

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://146755]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-09-30 23:32 GMT
Find Nodes?
    Voting Booth?

    How do you remember the number of days in each month?

    Results (386 votes), past polls