Python将datetime.time转换为箭头

时间:2016-11-07 21:00:38

标签: python datetime

我需要将python对象datetime.time转换为arrow对象。

y = datetime.time()
>>> y
datetime.time(0, 0)
>>> arrow.get(y)

TypeError: Can't parse single argument type of '<type 'datetime.time'>'

3 个答案:

答案 0 :(得分:4)

箭头遵循其文档中指定的特定格式:

arrow.get('2013-05-11T21:23:58.970460+00:00')  

您需要将日期时间对象转换为箭头可理解的格式,以便能够将其转换为箭头对象。下面的代码块应该有效:

from datetime import datetime
import arrow

arrow.get(datetime.now())

答案 1 :(得分:1)

您可以使用strptime类的arrow.Arrow类方法并应用相应的格式设置:

y = datetime.time()
print(arrow.Arrow.strptime(y.isoformat(), '%H:%M:%S'))
# 1900-01-01T00:00:00+00:00

但是,您会注意到日期值是默认值,因此您最好不要分析datetime对象而不是time对象。

答案 2 :(得分:0)

datetime.time对象包含“理想时间,与任何特定的日期无关”。箭头对象不能代表这种部分信息,因此您必须首先用今天的日期或其他日期“填写”。

from datetime import time, date, datetime
t = time(12, 34)
a = arrow.get(datetime.combine(date.today(), t))  # <Arrow [2019-11-14T12:34:00+00:00]>
a = arrow.get(datetime.combine(date(1970, 1, 1), t))  # <Arrow [1970-01-01T12:34:00+00:00]>