Parsing Worldtime API response to get just the local time in hours and minutes

Having got the “worldtimeapi” to respond with the date and time data I require, can anyone point me at a SIMPLE method of extracting just the local time hours and minutes from the wealth of information that world time provides in a form I can then manipulate within a Micropython programme, ie a simple string?

This is how I do it. It is plain Python (except the first line of course)

json_response = .... # response of worldtime-api converted to json: requests.get(url).json()
current_time = json_response["datetime"]
the_date, the_time = current_time.split("T")
year, month, mday = [int(x) for x in the_date.split("-")]
the_time = the_time.split(".")[0]
hours, minutes, seconds = [int(x) for x in the_time.split(":")]

Thank you as always for your prompt response. I shall give it a try (after I have spent some time trying to understand what the code is doing !!! (Oh, for a local Python course with real people of whom you can ask questions as they arise). Thanks again and I shall try to let you know of my success or otherwise.

1 Like