1

I'm trying to figure out a way to get the current time for an address or even just a state or postal code.

i.e. if it is 12:00 noon here (MN) what time is it in Phoenix AZ?

Ideally it would take into consideration daylight savings time.

I would like to do it without relying on an external web service.

John S
  • 7,909
  • 21
  • 77
  • 145

1 Answers1

5

It is impossible to do this in a generic way without relying on external web service. In fact you need two - first you need to geocode the address to a coordinate on the map. Then figure out the time zone and day light saving rules at the coordinate.

Google started providing time zone service recently, so this is relatively easy at small volumes. You need https://developers.google.com/maps/documentation/geocoding/ and https://developers.google.com/maps/documentation/timezone/ The new timezone api accepts timeStamp parameter and returns dstOffset, which can be converted to/from DateTime like so https://stackoverflow.com/a/7844741/467198

alternatively, there's non-google service here http://www.earthtools.org/webservices.htm which is provided as-is and can return formatted local time right away based on coordinates. You still need to geocode your address.

The answer that David mentioned here Mapping US zip code to time zone does simple mapping of state or zip code to time zone - you really need zip code though, as some states are not that simple (i.e. one state). And obviously this is a US-only solution.

Community
  • 1
  • 1
Artemiy
  • 1,969
  • 14
  • 19