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"
0

1 Answer 1

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
2
  • will this give me a UTC DateTime ?
    – Paras
    Commented Apr 4, 2019 at 8:18
  • Use def Local2UTC(LocalTime): method to convert to UTC time. Commented Apr 4, 2019 at 8:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.