Python 中的语言环境日期格式 [英] Locale date formatting in Python

查看:27
本文介绍了Python 中的语言环境日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以母语打印datetime.datetime.now()?

 >>>session.deathDate.strftime("%a, %d %b %Y")'2009 年 6 月 12 日,星期五'

我想得到相同的结果,但使用本地语言.

解决方案

你可以像这个例子一样设置语言环境:

<预><代码>>>>导入时间>>>打印时间.strftime("%a, %d %b %Y %H:%M:%S")2005 年 10 月 23 日,星期日 20:38:56>>>导入语言环境>>>locale.setlocale(locale.LC_TIME, "sv_SE") # 瑞典语'sv_SE'>>>打印时间.strftime("%a, %d %b %Y %H:%M:%S")sön, 23 okt 2005 20:39:15

How do I get datetime.datetime.now() printed out in the native language?

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

I'd like to get the same result but in local language.

解决方案

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

这篇关于Python 中的语言环境日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆