【问题标题】:Python - Convert 12 hour time in UTC in epoch formatPython - 以纪元格式转换 UTC 的 12 小时时间
【发布时间】:2018-04-03 20:24:29
【问题描述】:

我有一个以本地时间格式显示时间的文本框 - 04/03/2018 02:59:44 PM 我在 python 中使用 Selenium 来获取这个时间并将其(本地时间)转换为纪元时间(UCT)。但它正在转换为早 11 小时 30 分钟的时间(2018 年 4 月 3 日凌晨 3:29:44)。这是我的代码:

next_chk_dt = myDriver.find_element_by_xpath("(//input[@id='dateIDVisible'])[6]").get_attribute('value')
# displays 04/03/2018 02:59:44 PM if you print the value
temp_Time2 = datetime.datetime.strptime(next_chk_dt, '%m/%d/%Y %H:%M:%S %p')
epoch_Time1 = calendar.timegm(temp_Time2.timetuple())
print (epoch_Time1)
# You get 1522726184, which is incorrect. it should be 1522781984 

【问题讨论】:

  • 您当地的时区是?
  • 我的时区是美国东部时间

标签: python timestamp utc


【解决方案1】:

calendar.timegm() 从 UTC 转换为自纪元以来的秒数。

mktime() 将本地时间转换为纪元以来的秒数。

来源:https://docs.python.org/2/library/time.html

【讨论】:

    【解决方案2】:

    我根据上面的建议解决了这个问题

    from time import strptime
    from datetime import datetime
    mytm= ("04/03/2018 02:59:44 PM")
    fmt = ("%m/%d/%Y %I:%M:%S %p")
    epochDate = int(time.mktime(time.strptime(mytm, fmt)))
    print (epochDate)
    # ==> 1522781984
    

    【讨论】:

      猜你喜欢
      • 2019-09-26
      • 1970-01-01
      • 2020-05-19
      • 2021-12-31
      • 2022-07-06
      • 2023-03-27
      • 1970-01-01
      • 2019-12-03
      • 2019-11-14
      相关资源
      最近更新 更多