This month => 03 or March last month => 02 or February Month before last => 01 or January Month before => 12 or December (of last year) This code is not working: my $tm = localtime; my $this_month = sprintf("%04d%02d?", $tm->year+1900, ($tm->mon)+1); my $last_month = sprintf("%04d%02d?", $tm->year+1900, ($tm->mon)); my $last_month_1 = sprintf("%04d%02d?", $tm->year+1900, ($tm->mon)-1); my $last_month_2 = sprintf("%04d%02d?", $tm->year+1900, ($tm->mon)-2);