Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Day of week for an arbitary date using core modules

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

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 +d print "day of week is $day_of_week\n"; # "Sun" .. "Sat"

Replies are listed 'Best First'.
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."

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-06-19 08:12 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.