Victor Victor - 1 year ago 63
Python Question

How do I find the date format ?

I am receiving a json that prints time data '2016-04-15T02:19:17+00:00' I I cant seem to figure out the format of this unicode string.

I need to find a difference in time between then and now. The first step in that is to convert the string to structured format and Iam not able to find the format

fmt='"%Y-%m-%d %H:%M:%S %Z'
#fmt='%Y-%m-%d %H:%M:%S.%f'
print datetime.datetime.strptime(result_json['alert_time'], fmt)

I keep getting exception that it is not the same format

time data '2016-04-15T02:19:17+00:00' does not match format '"%Y-%m-%d %H:%M:%S %Z'

Answer Source

There are a few problems with your format. First, it has a double quote " in it. Second, you need to include the T between the date and the time. Third, the timezone offset is not standard. Here is code that will work:

print datetime.datetime.strptime('2016-04-15T02:19:17', '%Y-%m-%dT%H:%M:%S')

If your alert_time is always in GMT, you can just trim the timezone off before calling strptime.