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

Re: How to get last month date using Date::Calc

by haukex (Archbishop)
on Oct 12, 2018 at 10:55 UTC ( [id://1223912]=note: print w/replies, xml ) Need Help??


in reply to How to get last month date using Date::Calc

use warnings; use strict; use Date::Calc qw/Add_Delta_YM/; sub prevmonth { my ($year, $month) = @_; my ($newyear, $newmonth, undef) = Add_Delta_YM( $year, $month, 1, 0, -1 ); return [$newyear, $newmonth]; } use Test::More; is_deeply prevmonth(2003,8), [2003,7]; is_deeply prevmonth(2004,1), [2003,12]; done_testing;

Update: This is also possible with the core module Time::Piece. If you have a modern browser like the latest Firefox or Chrome, here is a live demo of the following code.

use warnings; use strict; use Time::Piece; while (<>) { chomp; my $t = Time::Piece->strptime($_, '%m/%Y'); my $prev = $t->add_months(-1); print $prev->strftime('%m/%Y'), "\n"; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1223912]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found