How to parse dates with -0400 timezone string in Python?

后端 未结 6 1374

I have a date string of the form \'2009/05/13 19:19:30 -0400\'. It seems that previous versions of Python may have supported a %z format tag in strptime for the trailing tim

6条回答
  •  星月不相逢
    2020-11-22 10:10

    One liner for old Pythons out there. You can multiply a timedelta by 1/-1 depending on +/- sign, as in:

    datetime.strptime(s[:19], '%Y-%m-%dT%H:%M:%S') + timedelta(hours=int(s[20:22]), minutes=int(s[23:])) * (-1 if s[19] == '+' else 1)
    

提交回复
热议问题