0

I need to know how to get the local time at a specific moment at any location on the globle.

Something like this:

DateTime localTime = GetLocalTime(utcDateTime, city);

or

DateTime localTime = GetLocalTime(utcDateTime, longitude, latitude);

Is there some API in the NET Fx, or Windows API or global web service to get the local time at a specific moment at a location on the globle?

Jesús López
  • 8,338
  • 7
  • 40
  • 66
  • 1
    Get a timezone: [How to get a time zone from a location using latitude and longitude coordinates?](http://stackoverflow.com/questions/16086962/how-to-get-a-time-zone-from-a-location-using-latitude-and-longitude-coordinates) and then get a time. – Ulugbek Umirov Jan 28 '16 at 07:31

1 Answers1

1

You can use Google API SDK to get the location. https://developers.google.com/maps/documentation/timezone/intro

public DateTime GetLocalTime(
DateTime utcDate, double latitude, double longitude )
{
var request = new TimeZoneRequest
{
ApiKey = _apiConfiguration.GoogleApiKey,
Location = new Location(latitude, longitude),
TimeStamp = utcDate,
Sensor = false
};

var response = GoogleMaps.TimeZone.Query(request);

if (response.Status != GoogleMapsApi.Entities.TimeZone.Response.Status.OK) 
return utcDate;

var offset = TimeSpan.FromSeconds(response.RawOffSet + response.OffSet);
var timeZone = TimeZoneInfo.CreateCustomTimeZone(response.TimeZoneId, offset, response.TimeZoneName, response.TimeZoneName);
var localTime = TimeZoneInfo.ConvertTimeFromUtc(utcDate, timeZone);

return localTime;
}
Radin Gospodinov
  • 2,313
  • 13
  • 14