http://www.perlmonks.org?node_id=1001445

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?

Replies are listed 'Best First'.
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 Athanasius (Archbishop) 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 Anonymous Monk on Oct 30, 2012 at 03:03 UTC
Re: How to find the last date of the previous month?
by tobyink (Canon) 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 (Beadle) 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 (Monk) 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";'