0

I'm attempting to get datetime information from the JSON I'm pulling from the API in a quick script I'm making in Python. When I go to grab the 'dt' value, it gives me such lovely results as 1628313960 or 1628314020. If you're a robot out there, I'm sure this is pretty good info, but to a human like me, I'm looking for something a little more along the lines of:

"dt_txt": "2020-08-05 13:00:00"

as it is showed in the API docs. I'm not able to find this key anywhere in the dictionary I've pulled. It does not appear to be there. This is what I've tried:

import requests, json


def get_48_hour_weather_data():

    url = f'https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&appid={api_key}&units=metric'

    response = requests.get(url)
    weather_data = json.loads(response.text)

    return weather_data

Does anybody know how to either:

  • Convert something like 1628313960 to something like "2020-08-05 13:00:00"
  • Get the 'dt_txt' value associated with the rest of this weather_data

Either solution would be great, and any help is appreciated! Thank you for reading, and I hope this an easy answer or reputation point for you if you are reading.

T.J. Kolberg
  • 47
  • 1
  • 6
  • Does this answer your question? [Converting unix timestamp string to readable date](https://stackoverflow.com/questions/3682748/converting-unix-timestamp-string-to-readable-date) – FObersteiner Aug 07 '21 at 16:54

1 Answers1

0

You can make a variable especially to record date and time, then use datetime from datetime library. () reportTime = datetime.datetime.utcfromtimestamp(weather_data["dt"]).strftime('%Y-%m-%d %H:%M:%S')