简体   繁体   English

将字符串转换为包含区域名称的有效日期时间对象

[英]Transforming a string to valid date time object that contain zone name

I have the following string 我有以下字符串

date = "Thu May 08 2014 12:06:43 GMT+0300 (EEST)"

How can I turn it in to a valid python datetime object using stptime? 如何使用stptime将其变成有效的python datetime对象?

I did this 我做了这个

datePy = datetime.strptime(date, "%a, %d %b %Y %H:%M:%S (%Z)")

but didn't work. 但是没有用 The traceback 追溯

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.7/_strptime.py", line 325, in _strptime
    (data_string, format))
ValueError: time data 'Thu May 08 2014 12:06:43 GMT+0300 (EEST)' does not match format '%a, %d %b %Y %H:%M:%S (%Z)'

For some background details I get the date string from javascript Date.toString() function send it to my django back end and want to turn it to python datetime object that is naive. 对于一些背景细节,我从javascript Date.toString()函数获取日期字符串,将其发送到django后端,并希望将其转换为天真的python datetime对象。

You aren't using the correct formatting (which is quite clear from the error). 您使用的格式正确 (从错误中可以很明显地看出来)。 For example, you have a , after the %a which isn't there in your string and %d (Day of the month as a zero-padded decimal number.) instead of %B (Month as locale's full name). 例如,你有一个,%a是不是有你的字符串和%d (天的月份,零填充十进制数。)代替%B (每月语言环境的全名)。 Try: 尝试:

datePy = datetime.strptime(date, "%a %B %d %Y %H:%M:%S (%Z)")

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

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