Jim Wang has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to get current and previous month in two digit number, (for example, if current month is July, I need 07 for it, and 06 for previous month). Is there a simple way in perl to do this? Thanks Jim

Replies are listed 'Best First'.
Re: get previous month in two digit number
by davido (Cardinal) on Jul 19, 2013 at 04:28 UTC

    With DateTime, you can:

    perl -MDateTime -E 'say DateTime->now->month' perl -MDateTime -E 'say DateTime->now->subtract(months=>1)->month'

    The output (we're currently in July):

    7 6

    Dave

Re: get previous month in two digit number
by Loops (Curate) on Jul 19, 2013 at 02:14 UTC
    use Date::Manip; print UnixDate('today', '%m'); # --> 07 print UnixDate('last month', '%m'); # --> 06
Re: get previous month in two digit number
by 2teez (Vicar) on Jul 19, 2013 at 04:01 UTC

    Or roll-out your own if you want, using localtime like so:
    A

    my $present_month = ( localtime() )[4] + 1; print sprintf "current month: %d\nprevious month: %d\n", $present_mont +h, $present_month - 1;
    A: UPDATE:
    Obviously, this breaks when the current month is January, because the previous month will then be "0", which doesn't translate to month "12" as we wanted.
    Please, use the tested module like Time::Piece and others as Loops and others had mentioned.
    'Am sorry I missed that big time...
    Loops++ for catching that.

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Rolling your own is often not the right answer though. It means that there are many implementations to update when things change. And unfortunately, the above code breaks when the current month is January, October, November or December.

      2teez,

      Don't be so hard on yourself. A simple test improves the script.

      $present_month - 1; if ( $present_month == 0 ) { $present_month = 12; }

      Now, if the OP needs other features of the referenced modules, then that's great, but if all he needs is your script, that's great also.

      Regards...Ed

      "Well done is better than well said." - Benjamin Franklin

      Just do

      ( $present_month - 1 || 12 )

      to implement rollover from January into December.

Re: get previous month in two digit number
by Anonymous Monk on Jul 19, 2013 at 06:00 UTC

    Hi,

    There are just under a million date libraries on CPAN.

    Have a squint at a few, find one that works in a way that you are comfortable with and give it a go.

    J.C.

Re: get previous month in two digit number
by ww (Archbishop) on Jul 19, 2013 at 13:25 UTC
    Mixed bag of answers above, but you didn't really provide enough info to answer the question in a manner calculated to deal with your source data and, thus, with your use case/problem.

    The key missing specific: in what form is your current month -- "July" or 07 or (month and month_day) "7/19" or are you deriving it from time or (any one of several other possibilities)?

    We can best provide help directed to your particular issue if you provide that kind of missing info.

    Apologies to all those electrons which were inconvenienced by the creation of this post.