Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How to find the last date of the previous month?

by MrSnrub (Sexton)
on Oct 30, 2012 at 02:39 UTC ( #1001445=perlquestion: print w/ replies, xml ) Need Help??
MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:

How do I find out the last day of the previous month? In other words, I want a subroutine that, when run today, will return "09/30/2012" (a string in mm/dd/yyyy format)? If the function is run a week from now it will return "10/31/2012"? And if the function was run sometime last March, it would return "02/29/2012" (leap-year day). How do I do this?

Comment on How to find the last date of the previous month?
Re: How to find the last date of the previous month?
by Anonymous Monk on Oct 30, 2012 at 03:03 UTC
Re: How to find the last date of the previous month?
by Athanasius (Prior) on Oct 30, 2012 at 03:26 UTC

    Here is one way:

    #! perl use strict; use warnings; use Date::Calc qw(Days_in_Month Today); print 'The last day of last month was ', last_day_last_month(), "\n"; sub last_day_last_month { my ($year, $month, $day) = Today([time]); if (--$month == 0) { $month = 12; --$year; } return sprintf("%02d/%d/%d", $month, Days_in_Month($year, $month), $year); }

    Hope that helps,

    Athanasius <°(((><contra mundum

Re: How to find the last date of the previous month?
by moritz (Cardinal) on Oct 30, 2012 at 05:27 UTC
Re: How to find the last date of the previous month?
by tobyink (Abbot) on Oct 30, 2012 at 08:11 UTC

    In the spirit of TIMTOWTDI, here's an example using Time::Piece. The advantage of using Time::Piece is that as of Perl 5.10, it's bundled with Perl core. (As is its companion module Time::Seconds.)

    use 5.010; use Time::Piece; use Time::Seconds qw( ONE_DAY ONE_HOUR ); sub last_day_of_last_month () { my $now = localtime; my $day = Time::Piece->new( $now - ($now->day_of_month * ONE_DAY) ); $day += ONE_HOUR * ($now->isdst <=> $day->isdst); return $day; } say last_day_of_last_month->mdy('/');

    The daylight savings calculation is a bit of an annoyance. :-(

    Yeah, that's right; I just did maths with the spaceship operator! :-)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to find the last date of the previous month?
by Anonymous Monk on Oct 30, 2012 at 10:44 UTC

    Steps:

    1. Find the epoch at 00.00 hrs for Date 1 of current month, current year.

    2. Subtract 1 from that to reach previous day and convert that epoch again into date.

    3. Grab the date and/or month value from it.

    #!/usr/bin/perl -w use strict; use Class::Date; my $date = new Class::Date [`date +%Y`, `date +%m`, 1, 0, 0, 0]; my $curMonthEpoch = $date->epoch; my $prevMonthEpoch = $curMonthEpoch - 1; print "Last Date of Prev Month=".`date --date='\@$prevMonthEpoch' +%d` +;
Re: How to find the last date of the previous month?
by MrSnrub (Sexton) on Oct 30, 2012 at 10:49 UTC
    Thanks all! These are great!
Re: How to find the last date of the previous month?
by larryl (Scribe) on Nov 04, 2012 at 20:08 UTC
    use DateTime; my $one_month_ago = DateTime->today->subtract(months => 1); print DateTime->last_day_of_month( month => $one_month_ago->month, year => $one_month_ago->year, )->mdy . "\n";'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001445]
Approved by Athanasius
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (8)
As of 2014-07-10 10:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (206 votes), past polls