Reputation: 199
How to get last month date using Date::Calc in perl?
For example, today is 8/2018 - I would like the function to return 7/2018.
If the date today is 1/2019 - it should return 12/2018
Upvotes: 1
Views: 1813
Reputation: 9231
Time::Moment provides a nice alternative as well:
use strict;
use warnings;
use feature 'say';
use Time::Moment;
say Time::Moment->now->minus_months(1)->strftime('%m/%Y');
Upvotes: 1
Reputation: 69284
Date::Calc seems to make everything far more complex than it needs to be:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Date::Calc qw[Today Add_Delta_Days];
my @today = Today;
my ($year, $month) = Add_Delta_Days(@today, -$today[2]);
say "$month/$year";
I'd recommend looking at DateTime instead.
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use DateTime;
my $today = DateTime->today;
$today->subtract(days => $today->day);
say $today->strftime('%m/%Y');
Upvotes: 1
Reputation: 1413
Try this below code
use Date::Calc qw(:all);
my ($current_year,$current_month,$current_day)=Today;
my ($result_year,$result_month,$result_day)=Add_Delta_YM($current_year,$current_month,$current_day,0,-1);
If you need fulll month name use below format
my $result_month_name=Month_to_Text($result_month);
Upvotes: 2