【问题标题】:How to convert datetime to timestamp Python如何将日期时间转换为时间戳 Python
【发布时间】:2012-06-01 22:55:47
【问题描述】:

我需要获取自创建元素以来经过了多少秒,该元素作为日期时间元素存储在数据存储区(Google App Engine)中。我正在使用日期时间变量。但我无法将时差转换为秒。 (Python 2.7)

这是我的目标:

def time_difference(datetime_time):
    return int(datetime.now()-datetime_time) #result should be in seconds like 612

非常感谢!

【问题讨论】:

    标签: google-app-engine python-2.7 unix-timestamp seconds


    【解决方案1】:

    减去日期时间会返回一个时间增量。在 Python 2.7 中,timedelta 有一个 total_seconds() 方法。

    def time_difference(datetime_time):
        delta = datetime.now() - datetime_time
        return int(delta.total_seconds())
    

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 2020-06-15
      • 1970-01-01
      • 2019-04-03
      • 2012-04-02
      • 2014-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多