Getting datetime.datetime.now() printed in the native language can be achieved using the strftime() function. However, if the application needs to support multiple locales, changing the locale (via locale.setlocale()) is not recommended. Instead, the Babel package provides a clean way to handle date/time formatting:
<code class="python">from datetime import date, datetime, time from babel.dates import format_date, format_datetime, format_time d = date(2007, 4, 1) print(format_date(d, locale='en')) # 'Apr 1, 2007' print(format_date(d, locale='de_DE')) # '01.04.2007'</code>
The Babel package offers the following advantages:
For more information, refer to the Date and Time section of Babel's documentation.
The above is the detailed content of How to Format Dates in Native Language with Python Using Babel?. For more information, please follow other related articles on the PHP Chinese website!