如何从timedelta获得价值?

时间:2014-12-05 18:26:34

标签: python sqlalchemy

当SQLalchemy返回时:

{u'age': datetime.timedelta(12045),}

我如何获得12045?我已经尝试了str()strftime()和其他一些人,但没有任何作用。

1 个答案:

答案 0 :(得分:3)

docs开始,使用.total_seconds()获取timedelta的长度。请注意,您提供的timedelta显示的是天数值,但timedelta也包括秒和微秒。如果您只想要这些日子,请使用.days

age = datetime.timedelta(12045)
print(age.total_seconds())  # 1040688000.0
print(age.days)  # 12045, but not the full value represented by the delta