Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

You can use strftime from POSIX to format the date, and timelocal from Time::Local to do basic time manipulation. Both are core modules, i.e. they come with Perl.

So, what I propose is the following steps:

  • with localtime, get the list of date and time values for today/now into an array
  • set the day of the month to the first, so you have the date of the first of this month. You may set the time to (around) noon to avoid problems with the switch of Daylight Saving Time, where a day is not exactly 24 hours, depending on what time of day it is now.
  • convert to seconds since epoch with timelocal
  • subtract 24*60*60 seconds to get a time in the last day of the past month as an integer value
  • with localtime, convert to the list of date and time values
  • use strftime to get the last day, properly formatted
  • in the array, set the day to the first.
  • use strftime to get the first day, properly formatted


use POSIX; use Time::Local; print daterange(); sub daterange { my @dt = localtime; $dt[3] = 1; $dt[2] = 12; my $time = timelocal @dt; $time -= 24*60*60; @dt = localtime $time; my $ret = strftime("%d %b %Y", @dt); $dt[3] = 1; return strftime("%d %b %Y", @dt) . " - $ret"; }

In reply to Re: How to get last month date period by bart
in thread How to get last month date period by roborat

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (3)
    As of 2017-12-12 01:09 GMT
    Find Nodes?
      Voting Booth?
      What programming language do you hate the most?

      Results (320 votes). Check out past polls.