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

Use DateTime:
use DateTime; my $today = DateTime->today(); my $last_month = $today->subtract( months => 1 ); my $first = $last_month->clone()->set_day(1); my $last = $first->add( months => 1 )->subtract( days => 1 ); print "first=$first last=$last\n";

Replies are listed 'Best First'.
Re^2: perl script to calculate beg and end of current month
by Ace28 (Initiate) on Jul 16, 2008 at 14:09 UTC
    Hi, I download this scirpt and ran it. It seems like set_day(1) is not working as expected. Currently, the first variable is showing as first=2008-06-30 instead of 2008-06-01. Let me know if there any workaround to fix this. Any help would be appreciated. -- Ace28
      This is how I managed to have the above code work.
      use DateTime; $dt = DateTime->today(); my $mnth = $dt->subtract(months => 1); my $first = $mnth->clone->set_day(1); my $last = $first->clone->add( months => 1 )->subtract( days => 1 ); print "first=$first last=$last\n";