perl -MDateTime -E 'say DateTime->now->month'
perl -MDateTime -E 'say DateTime->now->subtract(months=>1)->month'
The output (we're currently in July):
7
6
| [reply] [d/l] [select] |
use Date::Manip;
print UnixDate('today', '%m'); # --> 07
print UnixDate('last month', '%m'); # --> 06
| [reply] [d/l] |
Or roll-out your own if you want, using localtime like so:
A
my $present_month = ( localtime() )[4] + 1;
print sprintf "current month: %d\nprevious month: %d\n", $present_mont
+h,
$present_month - 1;
A: UPDATE:
Obviously, this breaks when the current month is January, because the previous month will then be "0", which doesn't translate to month "12" as we wanted. Please, use the tested module like Time::Piece and others as Loops and others had mentioned. 'Am sorry I missed that big time...
Loops++ for catching that.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
| [reply] [d/l] |
Rolling your own is often not the right answer though. It means that there are many implementations to update when things change. And unfortunately, the above code breaks when the current month is January, October, November or December.
| [reply] |
$present_month - 1;
if ( $present_month == 0 ) { $present_month = 12; }
Now, if the OP needs other features of the referenced modules, then that's great, but if all he needs is your script, that's great also.
Regards...Ed
"Well done is better than well said." - Benjamin Franklin
| [reply] [d/l] |
( $present_month - 1 || 12 )
to implement rollover from January into December.
| [reply] [d/l] |
Hi,
There are just under a million date libraries on CPAN.
Have a squint at a few, find one that works in a way that you are comfortable with and give it a go.
J.C.
| [reply] |
Mixed bag of answers above, but you didn't really provide enough info to answer the question in a manner calculated to deal with your source data and, thus, with your use case/problem.
The key missing specific: in what form is your current month -- "July" or 07 or (month and month_day) "7/19" or are you deriving it from time or (any one of several other possibilities)?
We can best provide help directed to your particular issue if you provide that kind of missing info.
Apologies to all those electrons which were inconvenienced by the creation of this post.
| [reply] [d/l] [select] |