How to get a list of months using python
April 2017
Sun Mon Tue Wed Thu Fri Sat
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30            
About
This site is an effort to share some of the base knowledge I have gathered through all this years working with Linux, FreeBSD, OpenBSD, Python or Zope, among others. So, take a look around and I hope you will find the contents useful.
Recent Entries
Recent Comments
Recent Trackbacks
Categories
OpenBSD (9 items)
BSD (0 items)
FreeBSD (19 items)
Linux (3 items)
Security (3 items)
Python (22 items)
Zope (13 items)
Daily (144 items)
e-shell (9 items)
Hacks (14 items)
PostgreSQL (3 items)
OSX (8 items)
Nintendo DS (0 items)
enlightenment (0 items)
Apache (3 items)
Nintendo Wii (1 items)
Django (24 items)
Music (12 items)
Plone (7 items)
Varnish (0 items)
Lugo (2 items)
Sendmail (0 items)
europython (7 items)
Cherokee (1 items)
self (1 items)
Nature (1 items)
Hiking (0 items)
uwsgi (0 items)
nginx (0 items)
cycling (8 items)
Networking (1 items)
DNS (0 items)
Archives

Syndicate this site (XML)

RSS/RDF 0.91

25 agosto
2008

How to get a list of months using python

.. dirty dirty dirty... ;D

This is a quick-and-dirty hack that came to my mind this morning. I needed a list where each element is a tuple containing (month number, month name). This little snippet will do the trick:

months_choices = []
for i in range(1,13):
    months_choices.append((i, datetime.date(2008, i, 1).strftime('%B')))

The result will be something like:

>>> months_choices = []
>>> for i in range(1,13):
...     months_choices.append((i, datetime.date(2008, i, 1).strftime('%B')))
...
>>> months_choices
[(1, 'January'), (2, 'February'), (3, 'March'), (4, 'April'), (5, 'May'), (6, 'June'), (7, 'July'), (8, 'August'), (9, 'September'), (10, 'October'), (11, 'November'), (12, 'December')]
>>>

The name of the month will be translated to you locale configuration (according to python documentation, you can take a look there to replace that with, for example, the abbreviated name). This is a perfect list to be used as the default list of options with a django ChoiceField or MultipleChoiceField form field ;D.

Posted by wu at 11:18 | Comments (4) | Trackbacks (0)
<< Trimmit | Main | Zope Security advisory 2008-08-12 >>
Comments
Re: How to get a list of months using python

Good job, very useful for me.

Posted by: Prasanna S at mayo 03,2009 09:24
Re: How to get a list of months using python

look at calendar.month_name :)
It's much simplier )

Posted by: Maxim Syabro at diciembre 30,2009 15:08
Re: How to get a list of months using python

Thnx for the tip Maxim. I didn't know about calendar.month_name.

In my example we could switch:


datetime.date(2008, i, 1).strftime('%B')


to:


calendar.month_name[i]


which is even cleaner. Thnx!

Posted by: Wu at diciembre 30,2009 15:18
Re: How to get a list of months using python

great job men.

Posted by: draix at febrero 09,2011 22:40
Re: How to get a list of months using python

now = datetime.datetime.now()

calendar.month_name[now.month]

Posted by: crev at marzo 28,2012 16:56
Re: How to get a list of months using python

I used your code from this and turned it into a list comprehension:

import datetime
months = [datetime.date(2000, m, 1).strftime('%m - %B') for m in range(1, 13)]

Posted by: DJC at agosto 25,2014 20:56
Re: How to get a list of months using python

Guys, try this

from django.utils.dates import MONTHS

Posted by: skoval at agosto 04,2015 13:07
Re: How to get a list of months using python

@skoval: sure, thanks for the hint!

OTOH: I wonder if django has that util back in 2009, when this post was written... :P

Posted by: Wu at agosto 08,2015 08:28
Trackbacks
Please send trackback to:http://blog.e-shell.org/94/tbping
There are no trackbacks.
Post a comment