【问题标题】:Calculate number of hours between two string timestamp python计算两个字符串时间戳python之间的小时数
【发布时间】:2020-06-28 00:17:52
【问题描述】:

如何计算两个时间戳之间的小时数具有以下格式:

'%Y-%m-%d %H:%M:%S %p' 作为我的数据框中的字符串

我如何使用 python 做到这一点??

例子:

开始时间: 2016-04-12 20:57:00 PM

结束时间: 2016-04-13 06:16:00 AM

【问题讨论】:

  • 你有没有尝试过,做过任何研究?你甚至没有提供示例输入和输出。

标签: python pandas numpy timestamp


【解决方案1】:

用途:

diff= (pd.to_datetime(end,format='%Y-%m-%d %H:%M:%S %p') -
       pd.to_datetime(start,format='%Y-%m-%d %H:%M:%S %p'))
hours = diff.total_seconds()//3600 # // rounds down
print(hours)
#9.0
#diff.total_seconds()/3600
#9.316666666666666

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2018-10-27
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多