in reply to Month Dates

This is fairly straightforward as long as you're not concerned with days of the month. Just roll your own code.

# set numeric values my $month = my $year = my $monthsback = # subtract from current month $month -= $monthsback; # convert to proper range of values if($month <= 0){$year--} $year += int($month/12); $month = $month%12; if ($month == 0){$month = 12}

Well, maybe a little more straightforward if you were looking for future months. If you need to take days of the month into account, you would have to figure out how you want to deal with such thinge as Feb 31st.

Replies are listed 'Best First'.
Re^2: Month Dates
by ikegami (Patriarch) on Mar 01, 2007 at 22:46 UTC

    It's odd that $month starts 0-based and ends 1-based. You also assume $monthsback is a small number. Fix:

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

    A more general solution:

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

      You're rigorous as usual ikegami, your realname wouldn't be Spock? ;)

      My point is that a simple algorithm could be used instead of importing a module, but I should have specified it's parameters more thoroughly:

      # positive integer, current year my $year = # integer 1 to 12, current month my $month = # positive integer, number of months to go back my $monthsback =