如何将“今天,9月7日下午12:45 - 01:00”转换为UTC

时间:2016-09-07 20:16:28

标签: python

我有以下时间字符串

12:45pm - 01:00pm Today, September 7

我希望以下列格式将此字符串转换为UTC日期时间

2016-09-07T22:45:00Z

如何在python中实现这一目标?

1 个答案:

答案 0 :(得分:0)

developed the script to achieve it.

    def appointment_time_string(time_str):
        import datetime
        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")) + "Z"

        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