What I am trying to do is to get the current datetime of a place using an API with python and extract datetime from it. The code that I have is:
import requests
import json
from datetime import datetime
import time
def get_current_time_from_api():
response = (requests.get("http://worldtimeapi.org/api/ip")).json()
return response["datetime"]
the_time = get_current_time_from_api()
When I print the response using print(the_time)
, the response that gets returned is:
2020-05-06T10:04:51.368291+05:45
Then, I tried getting the converting the string to datetime using the function datetime.strptime(the_time, "%X)
to get the time, I get the error ValueError: time data '2020-05-06T10:09:52.009222+05:45' does not match format '%X'
So, what went wrong and how can I do things like this when the time is parsed from the string?
if(time == "10:00:00 pm"):
#do something here
else:
difference_in_minutes = "10:00:00" - current_time
time.sleep(difference_in_minutes * 100) #sleeping for that many seconds
#do stuff when the time is 10 pm