Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: How to display 10 -1 =09 In Perl

by Not_a_Number (Prior)
on Oct 30, 2014 at 08:18 UTC ( [id://1105588]=note: print w/replies, xml ) Need Help??


in reply to How to display 10 -1 =09 In Perl

But what if your current month is 1?

Do you want your previous monnth to be 0 or 12?

Replies are listed 'Best First'.
Re^2: How to display 10 -1 =09 In Perl
by chai6125 (Initiate) on Oct 30, 2014 at 09:51 UTC
    Hi My Previous month should be 12 in that case and i have taken care of that. :) I did the following can you please tell it is correct
    my $current_month = strftime "%m", localtime; my $previous_month = $current_month - 1; my $previous_month = sprintf("%02d", $previous_month);

      Last month with Time::Piece with month granularity

      #!/usr/bin/perl -- use strict; use warnings; use Time::Piece qw/ localtime /; Main( @ARGV ); exit( 0 ); sub Main { my $now = localtime; my $lastmonth = $now->prevmonth; print "$now\n$lastmonth\n"; } sub Time::Piece::prevmonth { my $t= shift; if( ! ref $t ) { $t = localtime; } my $y = $t->year; my $m = $t->mon; #~ if( $m == 12 ){ $m=1; $y++; } else { $m++; } if( $m == 1 ){ $m=12; $y--; } else { $m--; } my $nt = Time::Piece->strptime( "$y-$m", '%Y-%m' ); } __END__ Thu Oct 30 03:10:05 2014 Mon Sep 1 00:00:00 2014

Log In?
Username:
Password:

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

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

    No recent polls found