简体   繁体   English

Python中的语言环境日期格式

[英]Locale date formatting in Python

How do I get datetime.datetime.now() printed out in the native language?如何以母语打印出datetime.datetime.now()

>>> session.deathDate.strftime("%a, %d %b %Y")
'Fri, 12 Jun 2009'

I'd like to get the same result but in local language.我想得到相同的结果,但使用当地语言。

If your application is supposed to support more than one locale then getting localized format of date/time by changing locale (by means of locale.setlocale() ) is discouraged.如果您的应用程序应该支持多个语言环境,则不鼓励通过更改语言环境(通过locale.setlocale() )获取日期/时间的本地化格式。 For explanation why it's a bad idea see Alex Martelli's answer to the the question Using Python locale or equivalent in web applications?有关为什么这是一个坏主意的解释,请参阅 Alex Martelli 对Using Python locale or equivalent in web applications? (basically locale is global and affects whole application so changing it might change behavior of other parts of application) (基本上语言环境是全局的,会影响整个应用程序,因此更改它可能会改变应用程序其他部分的行为)

You can do it cleanly using Babel package like this:你可以像这样使用 Babel 包干净地做到这一点:

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

>>> d = date(2007, 4, 1)
>>> format_date(d, locale='en')
u'Apr 1, 2007'
>>> format_date(d, locale='de_DE')
u'01.04.2007'

See Date and Time section in Babel's documentation.请参阅 Babel 文档中的日期和时间部分。

You can just set the locale like in this example:您可以像在此示例中那样设置语言环境:

>>> import time
>>> print time.strftime("%a, %d %b %Y %H:%M:%S")
Sun, 23 Oct 2005 20:38:56
>>> import locale
>>> locale.setlocale(locale.LC_TIME, "sv_SE") # swedish
'sv_SE'
>>> print time.strftime("%a, %d %b %Y %H:%M:%S")
sön, 23 okt 2005 20:39:15

You should use %x and %X to format the date string in the correct locale.您应该使用%x%X在正确的语言环境中格式化日期字符串。 Eg in Swedish a date is represented as 2014-11-14 instead of 11/14/2014 .例如,在瑞典语中,日期表示为2014-11-14而不是11/14/2014

The correct way to get the result as Unicode is:以 Unicode 格式获取结果的正确方法是:

locale.setlocale(locale.LC_ALL, lang)
format_ = datetime.datetime.today().strftime('%a, %x %X')
format_u = format_.decode(locale.getlocale()[1])

Here is the result from multiple languages:以下是多种语言的结果:

Bulgarian пет, 14.11.2014 г. 11:21:10 ч.
Czech pá, 14.11.2014 11:21:10
Danish fr, 14-11-2014 11:21:10
German Fr, 14.11.2014 11:21:10
Greek Παρ, 14/11/2014 11:21:10 πμ
English Fri, 11/14/2014 11:21:10 AM
Spanish vie, 14/11/2014 11:21:10
Estonian R, 14.11.2014 11:21:10
Finnish pe, 14.11.2014 11:21:10
French ven., 14/11/2014 11:21:10
Croatian pet, 14.11.2014. 11:21:10
Hungarian P, 2014.11.14. 11:21:10
Italian ven, 14/11/2014 11:21:10
Lithuanian Pn, 2014.11.14 11:21:10
Latvian pk, 2014.11.14. 11:21:10
Dutch vr, 14-11-2014 11:21:10
Norwegian fr, 14.11.2014 11:21:10
Polish Pt, 2014-11-14 11:21:10
Portuguese sex, 14/11/2014 11:21:10
Romanian V, 14.11.2014 11:21:10
Russian Пт, 14.11.2014 11:21:10
Slovak pi, 14. 11. 2014 11:21:10
Slovenian pet, 14.11.2014 11:21:10
Swedish fr, 2014-11-14 11:21:10
Turkish Cum, 14.11.2014 11:21:10
Chinese 周五, 2014/11/14 11:21:10

Another option is:另一种选择是:

>>> import locale
>>> import datetime
>>> locale.setlocale(locale.LC_TIME,'')
'es_CR.UTF-8'
>>> date_format = locale.nl_langinfo(locale.D_FMT)
>>> date_format
'%d/%m/%Y'
>>> today = datetime.date.today()
>>> today
datetime.date(2012, 4, 23)
>>> today.strftime(date_format)
'23/04/2012'

solution for russian language and cross platform俄语和跨平台解决方案

import sys
import locale
import datetime

if sys.platform == 'win32':
    locale.setlocale(locale.LC_ALL, 'rus_rus')
else:
    locale.setlocale(locale.LC_ALL, 'ru_RU.UTF-8')

print(datetime.date.today().strftime("%B %Y"))

Ноябрь 2017 Ноябрь 2017

This solution works in current Python 3.9 but also in Python 2.7.此解决方案适用于当前的 Python 3.9,但也适用于 Python 2.7。 There is no need to mess around with local .没有必要与local Just use the strftime() format strings %c (date and time), %x (date only) or %X (time only):只需使用strftime()格式字符串%c (日期和时间)、 %x (仅限日期)或%X (仅限时间):

>>> from datetime import datetime
>>> now = datetime.now()
>>> now.strftime('%c')
'So 29 Mai 2022 17:06:17 '
>>> now.strftime('%x')
'29.05.2022'
>>> now.strftime('%X')
'17:06:17'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM