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"
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
def Local2UTC(LocalTime):
method to convert to UTC time.
Commented
Apr 4, 2019 at 8:31