use Time::localtime; my $tm = localtime; my $this_month = sprintf("%04d%02d?", $tm->year+1900, ($tm->mon)+1); my $last_month = ($tm->mon == 0) ? sprintf("%04d12?", $tm->year+1899) : sprintf("%04d%02d?", $tm->year+1900, $tm->mon);