【问题标题】:Converting 12 hour format time string (with am/pm) into UTC in 24 hour format将 12 小时格式的时间字符串(带 am/pm)转换为 24 小时格式的 UTC
【发布时间】:2019-02-05 01:03:14
【问题描述】:

我有时间字符串11:15am11:15pm。 我正在尝试将此字符串转换为 24 小时格式的 UTC 时区。

从 EST 到 UTC

例如:当我通过11:15am 时,它应该转换为15:15,当我通过11:15pm 时,它应该转换为3:15

我有这段代码,我正在尝试:

def appointment_time_string(time_str):
    import datetime
    a = time_str.split()[0]

    # b =  re.findall(r"[^\W\d_]+|\d+",a)

    # c = str(int(b[0]) + 4) + ":" + b[1]
    # print("c", c)



    in_time = datetime.datetime.strptime(a,'%I:%M%p')

    print("In Time", in_time)

    start_time = str(datetime.datetime.strftime(in_time, "%H:%M:%S"))

    print("Start TIme", start_time)

    if time_str.split()[3] == 'Today,':
        start_date = datetime.datetime.utcnow().strftime("%Y-%m-%dT")
    elif time_str.split()[3] == 'Tomorrow,':
        today = datetime.date.today( )
        start_date = (today + datetime.timedelta(days=1)).strftime("%Y-%m-%dT")

    appointment_time = str(start_date) + str(start_time)

    return appointment_time

x = appointment_time_string(time_str)
print("x", x)

但这只是转换为 24 小时制,而不是 UTC。

【问题讨论】:

    标签: python python-2.x


    【解决方案1】:

    要将时间从 12 小时格式转换为 24 小时格式,您可以使用以下代码:

    from datetime import datetime
    new_time = datetime.strptime('11:15pm', '%I:%M%p').strftime("%H:%M")
    # new_time: '23:15'
    

    为了将时间从EST转换为UTC,最可靠的方法是使用第三方库pytz。更多详情请参考How to convert EST/EDT to GMT?

    【讨论】:

    • 我得到了这部分,现在我需要将它转换为 UTC 时区
    • 目前在哪个时区?
    • 它在 EST 时区并且想要 UTC
    • 添加了解决方案的链接。或者,您也可以将时间减去 12 小时,您会得到想要的结果
    【解决方案2】:

    使用提供的选项/解决方案开发了以下脚本以满足我的要求。

    def 约会时间字符串(time_str):

        import datetime
        import pytz
    
        a = time_str.split()[0]
    
        in_time = datetime.datetime.strptime(a,'%I:%M%p')
    
        start_time = str(datetime.datetime.strftime(in_time, "%H:%M:%S"))
    
        if time_str.split()[3] == 'Today,':
            start_date = datetime.datetime.utcnow().strftime("%Y-%m-%d")
        elif time_str.split()[3] == 'Tomorrow,':
            today = datetime.date.today( )
            start_date = (today + datetime.timedelta(days=1)).strftime("%Y-%m-%d")
    
        appointment_time = str(start_date) + " " + str(start_time)
    
        # print("Provided Time", appointment_time)
    
        utc=pytz.utc
        eastern=pytz.timezone('US/Eastern')
        fmt='%Y-%m-%dT%H:%M:%SZ'
        # testeddate = '2016-09-14 22:30:00'
    
        test_date = appointment_time
    
        dt_obj = datetime.datetime.strptime(test_date,'%Y-%m-%d %H:%M:%S')
        dt_str = datetime.datetime.strftime(dt_obj, '%m/%d/%Y %H:%M:%S')
        date=datetime.datetime.strptime(dt_str,"%m/%d/%Y %H:%M:%S")
        date_eastern=eastern.localize(date,is_dst=None)
        date_utc=date_eastern.astimezone(utc)
    
        # print("Required Time", date_utc.strftime(fmt))
    
        return date_utc.strftime(fmt)
    

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 1970-01-01
      • 2018-01-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-15
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      相关资源
      最近更新 更多