in reply to perl script to calculate beg and end of current month

Here's some code to generate the date range for the previous month. You'll want it formatted differently if using it for queries, however.
use strict; use warnings; my (@mon, $mon, $year); @mon = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); ($mon, $year) = (localtime(time))[4,5]; $year += 1900 if $year < 2000; if ($mon == 0) { $mon = 12; $year--; } if ($mon == 2) { if ($year % 4 != 0) { $mon[1] = 28; } elsif ($year % 400 == 0) { $mon[1] = 29; } elsif ($year % 100 == 0) { $mon[1] = 28; } else { $mon[1] = 29; } } $year = $year % 100; print sprintf('%02d/%02d/%02d', $mon, 1, $year), " through ", sprintf('%02d/%02d/%02d', $mon, $mon[$mon-1], $year);

Replies are listed 'Best First'.
Re^2: perl script to calculate beg and end of current month
by Anonymous Monk on Oct 31, 2012 at 17:40 UTC
    What about leap year? You hardcode the normal number of days in a month. This will cause rework as all cases are not coded for. Not sure of the answer just yet but came across this while researching it myself. Just saw the opportunities in the logic.