Or, if you only have core modules at your disposal, then you could work out the first of next month and subtract a day:

use strict; use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = localtime(); $mday = 1; $mon = ($mon + 1) % 12; $year += 1 if($mon == 0); my $time = timelocal($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print scalar localtime($time);

Update: Sorry, I completely misread the question and gave you the last day of this month. The last day of last month is a little easier:

use Time::Local; use constant ONE_DAY => 24 * 60 * 60; my ($sec,$min,$hour,$mday,$mon,$year) = gmtime(); $mday = 1; my $time = timegm($sec,$min,$hour,$mday,$mon,$year) - ONE_DAY; print "Date: " . localtime($time) . "\n"; $mday = (localtime($time))[3]; print "Day of month: $mday\n";

Update 2: As MarkM notes below, daylight savings changes could trip these snippets up. I originally coded them to set $sec, $min and $hour all to zero and then subtract say 4 hours - which I think should be pretty safe. I changed it before posting because I thought it was a bit obfuscated - but clear code that's wrong is no less wrong :-)


In reply to Re: How do i get "the last day of last month"? by grantm
in thread How do i get "the last day of last month"? by kiseok7

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.