0

I was using this code to print the month. In views.py

currentMonth = datetime.now().month return render_to_response('showroom.html',{'currentMonth':currentMonth,} , context_instance=RequestContext(request))

after that print the variable {{currentMonth}} in our file. It shows 12 but i want to show december.

  • This looks to be a duplicate of http://stackoverflow.com/questions/6557553/get-month-name-from-number – John Szakmeister Dec 13 '12 at 09:18
  • when i am using **import datetime currentMonth = datetime.now().month currentMonthn = currentMonth.strftime("%B")** It throughs **error** **'int' object has no attribute 'strftime'** – Rahul ghrix Dec 13 '12 at 09:48

1 Answers1

1

You want something like this:

# Near the top of views.py
from datetime import datetime

# ...
currentMonth = datetime.now().strftime('%B')
return render_to_response('showroom.html',{'currentMonth':currentMonth,} , context_instance=RequestContext(request))

In the code you have in your comment above:

import datetime
currentMonth = datetime.now().month
currentMonthn = currentMonth.strftime("%B")

currentMonth is an integer, and does not have the strftime method, but datetime.now() returns an object that does:

Python 2.6.1 (r261:67515, Jun 24 2010, 21:47:49) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.now().strftime('%B')
'December'
>>>
John Szakmeister
  • 44,691
  • 9
  • 89
  • 79