错误python日期时间/时间导入...

时间:2012-04-05 18:24:10

标签: python datetime

有错误

ValueError: time data '2012-03-15 9:45:00 AM' does not match format '%Y-%m-%d %H:%M:%S'

如何转换为datetime?问题是上午/下午,%I:%M:%S%p对我不起作用。

我需要将日期添加到time.strftime("%H:%M:%S", (0,0,0,8,56,0,0,0,0))

编辑二:尝试使用...

**

import time
from time import strftime
from datetime import date, time, datetime

** 按此顺序进行的这些导入会导致

出现问题

start_time = time.time()

start_time = time.time()
AttributeError: type object 'datetime.time' has no attribute 'time'

旧编辑......

datetime.combine(y='a date',z= 'a time')

datetime.date z是datetime.time

y=datetime(t.year, t.month, t.day)这解决了。

出了问题。

z是datetime.time

datetime.combine(y, datetime.time(18, 54, 32)

错误消息.............

    a =datetime.combine(y, datetime.time(18, 54, 32))
TypeError: descriptor 'time' requires a 'datetime.datetime' object but received a 'int'

3 个答案:

答案 0 :(得分:2)

查看dateutil包http://pypi.python.org/pypi/python-dateutil

from dateutil import parser
date = parser.parse("2012-03-15 9:45:00 AM")

答案 1 :(得分:1)

怎么样:

>>> import datetime
>>> datetime.datetime.strptime("2012-03-15 9:45:00 AM", "%Y-%m-%d %I:%M:%S %p")
datetime.datetime(2012, 3, 15, 9, 45)
>>> datetime.datetime.strptime("2012-03-15 9:45:00 PM", "%Y-%m-%d %I:%M:%S %p")
datetime.datetime(2012, 3, 15, 21, 45)

答案 2 :(得分:1)

关于更新:您从datetime模块导入的内容很混乱。正确的样本是:

>>> from datetime import date, time, datetime
>>> d=date(2012,10,01)
>>> t=time(10,11,12)
>>> datetime.combine(d,t)
datetime.datetime(2012, 10, 1, 10, 11, 12)