Google应用引擎数据存储日期时间到Python为止? [英] Google app engine datastore datetime to date in Python?
问题描述
2012-06-25 01:17:40.273000
我尝试了
datetime.strptime('2012-06-25 01:17:40.273000','%y-%m-%d%H:%M:%S')
但它没有工作..
我只想提取 2012-06-25
部分,而不使用黑客正则表达式或字符串切片解决方案。
我如何解析并将其转换为正确的格式?
如果您使用的是datetime属性,那么您返回的对象是datetime实例,而不是字符串。
在控制台上
>>> from datetime import datetime
>>>> x = datetime.now()
>>>>打印x
2012-06-25 12:03:15.835467
>>> x.date()
datetime.date(2012,6,25)
>>>
>>> print x.date()
2012-06-25
>>>
请参阅print语句。它将隐式转换为字符串。如果你在模板中输出datetime属性值,这可能是发生了什么。
所以在你的代码中,你应该在datetime对象上使用.date()方法。
I've always hated the headache of managine dates, times, datetimes, and the various formats and conversions that are needed with them. I'm taking an online course on using the google app engine and it says to use the datetime property, which is returning a date in the format:
2012-06-25 01:17:40.273000
I tried
datetime.strptime('2012-06-25 01:17:40.273000','%y-%m-%d %H:%M:%S')
but it didn't work..
I just want to extract the 2012-06-25
part without using a hacky regex or string slicing solution.
How do I parse this and convert it to the proper format?
If you are using a datetime property then the object you get back is a datetime instance not a string.
On the console
>>> from datetime import datetime
>>> x = datetime.now()
>>> print x
2012-06-25 12:03:15.835467
>>> x.date()
datetime.date(2012, 6, 25)
>>>
>>> print x.date()
2012-06-25
>>>
See the print statement. It does an implicit conversion to string. If your outputting the datetime property value in a template, this is probably what is happening.
So in your code you should just use .date() method on the datetime object.
这篇关于Google应用引擎数据存储日期时间到Python为止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!