Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Calc last friday of month

by csarid (Sexton)
on Feb 03, 2009 at 04:37 UTC ( #740897=perlquestion: print w/replies, xml ) Need Help??
csarid has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I would like to know if perl has a way to calculate the "first monday of the month" or the "last friday of the month" etc.. Thanks!

Replies are listed 'Best First'.
Re: Calc last friday of month
by Your Mother (Chancellor) on Feb 03, 2009 at 04:50 UTC
Re: Calc last friday of month
by ikegami (Pope) on Feb 03, 2009 at 04:50 UTC
    1. Find the day of the week of the first day of the month (using Date::Calc, DateTime, localtime or whatever).

    2. Find the number of days to add or subtract.

      Days to first Monday:

      $days_to_first_monday = ($dow + 7 - 1) % 7;

      Days to previous Friday:

      $days_to_last_friday = ($dow + 7 - 5) % 7 - 7;

      In both case, $dow is expected to hold 1..7 for Mon..Sun or 0..6 for Sun..Sat

    3. Use Date::Calc or DateTime to perform the math.

Re: Calc last friday of month
by CountZero (Bishop) on Feb 03, 2009 at 06:24 UTC
    use strict; use Date::Manip; $main::TZ= 'GMT'; print UnixDate(ParseDate("last Friday in February 2009"),"Last Friday +of the month is %B %E, %Y.");


    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      Wow! the Date::Manip is powerful, this is exactly what I was looking for. Thanks to every for their replies. I will definitely use the methods suggested at some point. Thanks very much!
Re: Calc last friday of month
by poolpi (Hermit) on Feb 03, 2009 at 09:03 UTC

    Some examples :

    #!/usr/bin/perl use strict; use warnings; use DateTime; my $dt = DateTime->last_day_of_month( year => 2009, month => 2 ); while ( $dt->mday() >= 1 and $dt->month == 2 ) { $dt->subtract( days => 1 ); print "Last Friday of February: ", $dt->ymd, "\n" and last if $dt->day_of_week == 5; } my $dt1 = DateTime->new( year => 2009, month => 1, day => 1 ); while (1) { $dt1->add( days => 1 ); print "First Monday of January: ", $dt1->ymd, "\n" and last if $dt1->day_of_week == 1; }


    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://740897]
Approved by ikegami
LanX my favourite don't tell me the proof of concept is good enough for production anecdote
[ambrus]: ah, it's one of thos
[Eily]: what, there's a difference between proof of concept and production?
[LanX]: 20 years ago traders were complaining about the latency of the trading system...
[ambrus]: I'm currently in the process of rewriting my proof of concept programs. They sort of developped organically as I was experimenting, so now I've got an ugly mess of multiple programs and one-liners held together by nothing. I'll have to rewrite them to som
[ambrus]: ething that's both cleanly organized and mostly automated.
LanX in train, bad connection
[Corion]: ambrus: Yeah - we're in that situation too, except that there is no time to do the reorganizing :-/
[LanX]: ... so my boss started a project with the newest sun servers and invited the traders to come on weekend to test it... and they were so pleased, that they forced him to keep it in production...
[ambrus]: Corion: sure, this is the long-term plan. The short term is that I have to run this ungodly mess to get results from the new input data today.

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (15)
As of 2017-03-29 11:47 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (350 votes). Check out past polls.