use POSIX qw( floor strftime ); my $monthsback = 3; # 0=this month -1=last, -2, ... my ($day,$mon,$year) = (localtime())[3,4,5]; $mon -= $monthsback; $year += floor($mon / 12); $mon = $mon % 12; print(strftime("%Y-%m", 0,0,0, $day,$mon,$year), "\n"); #### use POSIX qw( floor strftime ); my $mon_delta = -3; # ..., -2, -1=last, 0=this month +1=next, +2, ... my ($day,$mon,$year) = (localtime())[3,4,5]; $mon += $mon_delta; $year += floor($mon / 12); $mon = $mon % 12; print(strftime("%Y-%m", 0,0,0, $day,$mon,$year), "\n");