aguyngueran aguyngueran - 4 years ago 159
Python Question

Babel date_format returning 2016 for 2017 date

I'm trying to display the date formatted for the specified language and I'm little surprised to see that:

babel.dates.format_date(date(2017,1,1), 'MMM Y')


gives the expected value which is:

u'Jan 2017'


while:

babel.dates.format_date(date(2017,1,1), 'MMM Y', locale='fr_FR')


gives me:

u'janv. 2016'


Why is it 2016 not 2017?

Answer Source

This is because Y is giving you the week year. You need to use yyyy to get what you expect.

See https://github.com/python-babel/babel/issues/138

print format_date(date(2017,1,1), 'MMM yyyy')

print format_date(date(2017,1,1), 'MMM yyyy', locale='fr_FR')

Jan 2017
janv. 2017
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download