Menu Close

How to do locale date formatting in Python?

Sometimes, we want to do locale date formatting in Python.

In this article, we’ll look at how to do locale date formatting in Python.

How to do locale date formatting in Python?

To do locale date formatting in Python, we can use the babel package.

To install it, we run

pip install Babel

Then we use it by writing

from datetime import date, datetime, time
from babel.dates import format_date, format_datetime, format_time

d = date(2020, 4, 1)
fd = format_date(d, locale='en')

to call format_date with date d and locale set to 'en' to return a date string that’s formatted for the English locale.

Conclusion

To do locale date formatting in Python, we can use the babel package.

Posted in Python, Python Answers