BEGINNER: converting integer to short month name?

Robin Munn rmunn at pobox.com
Thu Apr 10 19:43:00 CEST 2003


MCollins at co.seminole.fl.us <MCollins at co.seminole.fl.us> wrote:
> that works fine for the numerical month names.  now i'm trying to convert
> the numerical month into it's short month name equivalent to grab those
> files.
> 
> i could always use a long if , elif, elif to do the job, but i was
> wondering if there was built-in function already capable of that.  i was
> just looking for something a little more elegant.

Try the time.strftime function:

    http://www.python.org/doc/2.2/lib/module-time.html

You might have to construct a tuple:

    time_tuple = (2000, the_month_num, 1, 0, 0, 0, 0, 0, 0)
    month_abbr = time.strftime('%b', time_tuple)

This will get you 'Jan', 'Feb', 'Mar', etc. depending on the value of
the_month_num.

Interesting, looks like you don't have to even specify a valid year or
day:

    the_month_num = 2
    time_tuple = (0, the_month_num, 0, 0, 0, 0, 0, 0, 0)
    month_abbr = time.strftime('%b', time_tuple)
    print month_abbr

prints out "Feb".

-- 
Robin Munn <rmunn at pobox.com>
http://www.rmunn.com/
PGP key ID: 0x6AFB6838    50FF 2478 CFFB 081A 8338  54F7 845D ACFD 6AFB 6838




More information about the Python-list mailing list