Here's an example using Date::Calc.

This code gets the current day, month and year using Date::Calc::Localtime, rather than the built in localtime().

This code will figure out the number of days in each month.
Leap years are handled correctly.

You could always modify this code so that you can get those numbers yourself outside of the subroutine and have the sub just do the calculations.

#!/usr/bin/perl use strict; use warnings; use Date::Calc qw/Localtime Days_in_Month/; sub give_dates { my ($year, $month, $day) = (Localtime())[0,1,2]; my $days_in_mon = Days_in_Month($year, $month); my $cutoff = int($days_in_mon / 2); my ($start, $end); if ( $day <= $cutoff) { # First half of month $start = $year . sprintf("%02d", $month) . '01'; $end = $year . sprintf("%02d", $month) . $cutoff; } else { # Second half of month $start = $year . sprintf("%02d", $month) . $cutoff; $end = $year . sprintf("%02d", $month) . $days_in_mon; } return ($start, $end); } print join " ", give_dates(), "\n";

Cheers.

BazB

Update: Corrected code. Initial version gave dates in wrong format (YYYYMMMDD not YYYYMMDD).


If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
That way everyone learns.


In reply to Re: date span in YYYYMMDD by BazB
in thread date span in YYYYMMDD by silent11

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.