简体   繁体   English

如何将以下字符串转换为python date?

[英]How to convert the following string to python date?

How can I convert: u'2012-11-07T13:25:10.703Z' to Python datetime? 如何将u'2012-11-07T13:25:10.703Z'转换为Python日期时间?

EDIT 编辑

I intend to use something like this: 我打算使用这样的东西:

>>> from datetime import datetime
>>> datetime.strptime('2011-03-07','%Y-%m-%d')
datetime.datetime(2011, 3, 7, 0, 0)

but how can I change the second argument to accommodate my date format? 但是如何更改第二个参数以适应日期格式?

Use datetime.datetime.strptime : 使用datetime.datetime.strptime

datetime.datetime.strptime(u'2012-11-07T13:25:10.703Z', '%Y-%m-%dT%H:%M:%S.%fZ')

Result: 结果:

datetime.datetime(2012, 11, 7, 13, 25, 10, 703000)

See the description of the strptime behaviour . 请参阅strptime行为的描述。

Use strptime from the datetime module 使用datetime模块中的strptime

import datetime
datetime.strptime(u'2012-11-07T13:25:10.703Z', '%Y-%m-%dT%H:%M:%S.%fZ')
>>> datetime.datetime(2012, 11, 7, 13, 25, 10, 703000)

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

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