Using only modules included with Perl...

Relative to current date:

use Time::Local qw( timelocal_nocheck ); my @time_data; @time_data = localtime(); $time_data[4]++; # Next month. $time_data[3] = 0; # Last day of previous month. @time_data = localtime(timelocal_nocheck(@time_data)); my $year = $time_data[5]+1900; my $month = $time_data[4]+1; # Make 1-based my $last_day = $time_data[3]; printf("The last day of this month (%04d/%02d) is %d.\n", $year, $month, $last_day ); __END__ output ====== The last day of this month (2005/01) is 31.

Relative to a specified year and month:

use Time::Local qw( timegm_nocheck ); my $year = 2005; for my $month (1..12) { my $last_day = (gmtime(timegm_nocheck( 0, 0, 0, 1-1, # Last day of previous month. $month-1+1, # Next month (0-based). $year )))[3]; printf("The last day of %04d/%02d is %d.\n", $year, $month, $last_day ); } __END__ output ====== The last day of 2005/01 is 31. The last day of 2005/02 is 28. The last day of 2005/03 is 31. The last day of 2005/04 is 30. The last day of 2005/05 is 31. The last day of 2005/06 is 30. The last day of 2005/07 is 31. The last day of 2005/08 is 31. The last day of 2005/09 is 30. The last day of 2005/10 is 31. The last day of 2005/11 is 30. The last day of 2005/12 is 31.

In reply to Re: Finding End of Month's date by ikegami
in thread Finding End of Month's date by neeraj

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.