Categories
Python Answers

How to do locale date formatting in Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *