0

I have an API response which returns a date time object in String. I need to convert it into a UTC Datetime object to compare with the current datetime.

How do I convert this to an UTC DateTime object?

received    "2019-03-22T06:35:57Z"
Paras
  • 3,191
  • 6
  • 41
  • 77

1 Answers1

0

Parse the string and convert to datetime using strptime.

import datetime
dateob =  datetime.datetime.strptime ("2019-03-22T06:35:57Z", "%Y-%m-%dT%H:%M:%SZ")

To convert to UTC:

>>> def Local2UTC(LocalTime):
...     EpochSecond = time.mktime(LocalTime.timetuple())
...     utcTime = datetime.datetime.utcfromtimestamp(EpochSecond)
...     return utcTime