how to get the current time for any city/country by using timezone property from OpenWeather API in Flutter?
779
A way to get current time of the city using the timezone
you've got from the API can be as following:
DateTime.now().add(Duration(seconds: timezone - DateTime.now().timeZoneOffset.inSeconds))
You will get a DateTime
object from here.
Author by
Ουιλιαμ Αρκευα
Updated on December 18, 2022Comments
-
Ουιλιαμ Αρκευα over 1 year
I got this JSON response by using OpenWeather's API:
{ "coord": { "lon": 139.69, "lat": 35.69 }, "weather": [ { "id": 803, "main": "Clouds", "description": "broken clouds", "icon": "04d" } ], "base": "stations", "main": { "temp": 306.63, "feels_like": 311.16, "temp_min": 304.82, "temp_max": 308.15, "pressure": 1011, "humidity": 63 }, "visibility": 10000, "wind": { "speed": 3.1, "deg": 140 }, "clouds": { "all": 75 }, "dt": 1598419499, "sys": { "type": 1, "id": 8077, "country": "JP", "sunrise": 1598386129, "sunset": 1598433465 }, "timezone": 32400, "id": 1850144, "name": "Tokyo", "cod": 200 }
I wish to get the current time from that city by using
timezone
property or another property. Is it possible? how?-
Yadu over 3 yearsDoes this answer your question? How to convert DateTime into different timezones?, look at the packages referred in this question
-
-
l_b over 3 yearsThe city variable is the city name which you will get from JSON Code and then put it in as a String.