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

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);

Replies are listed 'Best First'.
Re^3: How to display 10 -1 =09 In Perl (Time::Piece)
by Anonymous Monk on Oct 30, 2014 at 10:02 UTC

    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