Google应用引擎数据存储日期时间到Python为止? [英] Google app engine datastore datetime to date in Python?

查看:102
本文介绍了Google应用引擎数据存储日期时间到Python为止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直很讨厌管理日期,时间,数据时间以及他们需要的各种格式和转换的头痛。我正在使用一个在线课程,使用谷歌应用程序引擎,它说要使用datetime属性,它返回的格式如下:



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屋!

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