【问题标题】:Comparing two times in Hours using python使用python在小时内比较两次
【发布时间】:2020-02-12 02:23:57
【问题描述】:

我有两个日期的时区格式为 ('%Y-%m-%dT%H:%M:%SZ')

Given_Time = '2020-02-12T02:12:12Z'
current_time = '2020-02-11T06:22:42Z' 

我想在 Hrs 中比较这两次。我的意思是给定日期比当前时间晚多少小时

【问题讨论】:

    标签: python-3.x date time utc


    【解决方案1】:
    import datetime
    def time_diff(x, y):
         x = datetime.datetime.strptime(x,'%Y-%m-%dT%H:%M:%SZ')
         y = datetime.datetime.strptime(y,'%Y-%m-%dT%H:%M:%SZ')
         return (x-y).total_seconds()/3600
    
    time_diff(Given_Time, current_time)
    19.825
    time_diff(current_time, Given_Time)
    -19.825
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-26
      • 2021-11-18
      相关资源
      最近更新 更多