I am programming an application that should give the local time based on the coordinates (lat&long) that you give it.
I only know of 2 methods to do that:
1st: Get the TimeZone Name, and then find its local time. 2nd: Use the Google API and receive the time as an offset and UTC not Local.
I decided to use the 1st method because seemed easier, so I decided to use the GeoTimeZone to get the Time Zone... Problem is that then I don´t know how to get the local time on that TimeZone... Here´s the code I wrote to get the TimeZone name.
string tz = TimeZoneLookup.GetTimeZone(lat, lon).Result;
variables lat
& lon
are of course the coordinates.
Thank you!
Edit: My question is how can I get the LocalTime on that TimeZone?
Here's how longitude converts into your personal time zone: 15 degrees of longitude = 1 hour difference; 1 degree longitude = 4 minutes difference. 15 minutes of longitude = 1 minute difference; 1 minute of longitude = 4 seconds difference.
Time zones are another arbitrary societal choice, like the origin point of the Prime Meridian. They are based on longitude and defined by Earth's rotation, which completes a full circle (360 degrees) each day (24 hours). Each hour then, Earth rotates through 360/24 = 15° of longitude: the width of one time zone.
Both the latitude and longitude parts together are the coordinates. In other words, where you say: This is the coordinate of the point.
Here's my solution. It works offline (so no call to an api). It's fast and the packages are widely used and available on Nuget.
string tzIana = TimeZoneLookup.GetTimeZone(lat, lng).Result;
TimeZoneInfo tzInfo = TZConvert.GetTimeZoneInfo(tzIana);
DateTimeOffset convertedTime = TimeZoneInfo.ConvertTime(DateTimeOffset.UtcNow, tzInfo);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With