在Windows中使用Python获取日期格式 [英] Get date format in Python in Windows

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

问题描述

在为我提供的用于家庭作业的示例代码中,此行出现:

In a sample code given to me for my homework, this line appears:

date_format = locale.nl_langinfo(locale.D_FMT)

但是在Windows中,该行返回以下错误:

But in Windows that line returns the following error:

File "C:\Users\Shadark\Dropbox\IHM\P3\p3_files\www\cgi-bin\todolist.py", line 11, in <module>
date_format = locale.nl_langinfo(locale.D_FMT)
AttributeError: 'module' object has no attribute 'nl_langinfo'

我已经读过有关使用 localeconv 的信息,但是我只读过有关使用货币或数字的信息.对用于我的代码示例或其他类型功能的用途有任何想法吗?

I've read about using localeconv but I only read about it being used currency or numbers. Any idea on uses for the purpose of my code sample or other kind of function?

谢谢.

推荐答案

您的问题可能是locale.nl_langinfo在Windows Python 2.7.x中似乎不可用的事实(我没有在副本中看到它) Windows 64位Python 2.7.3).在 http://docs.python.org/2.7/上查看文档library/locale.html#locale.nl_langinfo ,他们专门说:

Your problem is probably the fact that locale.nl_langinfo doesn't appear to be available in Windows Python 2.7.x (I don't see it in my copy of Windows 64-bit Python 2.7.3). Looking at the docs at http://docs.python.org/2.7/library/locale.html#locale.nl_langinfo, they specifically say:

此功能并非在所有系统上都可用,并且可能的选项集也可能因平台而异.

This function is not available on all systems, and the set of possible options might also vary across platforms.

设置语言环境后,可以按照以下方式进行操作:

Once you've set the locale up with something along the lines of:

locale.setlocale(locale.LC_ALL, 'english')

然后调用some_date.strftime()将使用正确的特定于语言环境的格式和字符串.因此,如果您要使用字符串格式的日期,请调用some_date.strftime('%x')%x替换为%X来表示时间,或者将%c都替换成这两个日期.在此处中记录了strftime格式的完整列表.

Then calls to some_date.strftime() will use correct locale specific formatting and strings. So if you want the date in string format, call some_date.strftime('%x') replace the %x with %X for time or %c for both. The full list of strftime formats are documented here.

>>> d = datetime.datetime.now()
... for loc in ('english', 'german', 'french'):
...     locale.setlocale(locale.LC_ALL, loc)
...     print loc, d.strftime('%c -- %x -- %X -- %B -- %A')
english 11/15/2012 4:10:56 PM -- 11/15/2012 -- 4:10:56 PM -- November -- Thursday
german 15.11.2012 16:10:56 -- 15.11.2012 -- 16:10:56 -- November -- Donnerstag
french 15/11/2012 16:10:56 -- 15/11/2012 -- 16:10:56 -- novembre -- jeudi
14: 'French_France.1252'

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

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