How to get the Timezone from a Latitude/Longitude Coordinates

Map of current official time zones
Map of current official time zones

When your website works internationally, you need to get the local time, for example, for scheduling appointments and meetings. That's why figuring out the time zone for a location can be a big help.

However, there are several problems with time zones. First, the time zone geometries may be too heavy for web and mobile apps. Second, time zones are changing, so you must update them frequently.

In this article, you will learn how to find the Time Zone Information of a location by its latitude and longitude coordinates with Geocoding API and how to get a local time for the location.

Getting Timezone

Timezones are represented by regions of the world, where all locations within a region fall under the same timezone. So, actually, all you need to do is find out what region the location belongs to.

You can use publicly available databases to determine the timezone by location. For example, Time Zone Database is one of the most popular. And there are libraries such as geo-tz that work with Time Zone Database and offer a convenient interface. But they are not all intended for use in the browser or mobile app since large files are included in each package to perform exact geographic lookups.

Geoapify's Geocoding API can help you with this! The Geocoding API returns location and time zone information for a specified postal address. The Reverse Geocoding API returns location, address, and timezone by coordinates.

Let's go over examples of how to use the API.

Timezone by lat/long coordinates

You can use the Reverse Geocoding API to get the timezone for an address by its latitude and longitude coordinates. Please note that you need an API key to make requests to the API. Registering and generating an API key can get started with our API. We have a Free tier that includes 3000 requests/day.

Here's an example of an API request:

https://api.geoapify.com/v1/geocode/reverse?lat=46.73917926727481&lon=2.3276588684885837&format=json&apiKey=YOUR_API_KEY

It's an HTTP Get request, which means you can execute it with most programming languages and no-coding platforms. You can find code samples that make HTTP requests by googling "How to make HTTP Get request with YOUR_LANGUAGE HERE."

Here is an example that shows how to get a timezone with javascript:

fetch(`https://api.geoapify.com/v1/geocode/reverse?lat=${lat}&lon=${lon}&format=json&apiKey=YOUR_API_KEY`)
.then(resp => resp.json())
.then((result) => {
  if (result.length) {
	  console.log(result[0].timezone);
  } else {
    console.log("No location found");
  }
});

Here is the result object returned by the API call:

{ 
  "country":"France",
  "housenumber":"126",
  "street":"Le Danger",
  "county":"Cher",
  "postcode":"18170",
  "city":"Morlac",
  "lon":2.319871,
  "lat":46.734568,
  "formatted":"126 Le Danger, 18170 Morlac, France",
  "timezone":{
      "name":"Europe/Paris",
      "offset_STD":"+01:00",
      "offset_STD_seconds":3600,
      "offset_DST":"+02:00",
      "offset_DST_seconds":7200,
      "abbreviation_STD":"CET",
      "abbreviation_DST":"CEST"
  },
  ...
}

Time zone information includes the name of the timezone, offset from UTC as a string, and in seconds for standard (STD) and daylight saving time (DST).

Timezone by address

If you don't have location coordinates for your data but only a postal address, you can still use the Geocoding API to get the timezone.

Here is an example of the API call URL where the URL encoded address (Carrer del Pintor Navarro Llorens, 7, 46008 València, Valencia, Spain) provided in the text parameter:

https://api.geoapify.com/v1/geocode/search?text=Carrer%20del%20Pintor%20Navarro%20Llorens%2C%207%2C%2046008%20Val%C3%A8ncia%2C%20Valencia%2C%20Spain&format=json&apiKey=YOUR_API_KEY

The result includes the corresponding location coordinates, timezone, and structured address:

{
  "housenumber": "7",
  "street": "Carrer del Pintor Navarro Llorens",
  "suburb": "Extramurs",
  "city": "Valencia",
  "postcode": "46008",
  "country": "Spain",
  "lon": -0.3874807,
  "lat": 39.4669989,
  "formatted": "Carrer del Pintor Navarro Llorens, 7, 46008 Valencia, Spain",
  "timezone": {
    "name": "Europe/Madrid",
    "offset_STD": "+01:00",
    "offset_STD_seconds": 3600,
    "offset_DST": "+02:00",
    "offset_DST_seconds": 7200,
    "abbreviation_STD": "CET",
    "abbreviation_DST": "CEST"
  },
  ...
}

Similar to the Reverse Geocoding API, the Geocoding API works via HTTP Get. Here is an example of how to call the API with Javascript:

const address = 'Carrer del Pintor Navarro Llorens, 7, 46008 València, Valencia, Spain';

fetch(`https://api.geoapify.com/v1/geocode/search?text=${encodeURIComponent(address)}&apiKey=YOUR_API_KEY`)
.then(resp => resp.json())
.then((geocodingResult) => {
	console.log(geocodingResult);
});

Find the Location's Local Time

Now, let's see how to find the local time for a given timezone.

Even though we have an offset for the timezone, the existing Daylight Saving Time complicates things. So, you need to find out if it's daylight saving time or not and then add the appropriate offset to the UTC value.

You can always refer to the Daylight Time Savings schedules, which are posted publicly. Here is the time switching table for the next years:

YearDST beginsDST ends
2022Sunday, March 13, 2:00 amSunday, November 6, 2:00 am
2023Sunday, March 12, 2:00 amSunday, November 5, 2:00 am
2024Sunday, March 10, 2:00 amSunday, November 3, 2:00 am
2025Sunday, March 9, 2:00 amSunday, November 2, 2:00 am

The general algorithm for calculating local time is as follows:

if DST then UTC + offset_DST else UTC + offset_STD

However, we suggest that you use libraries to get local date time strings. The libraries consider DST inside, so you don't need to care about it.

Here is a JavaScript code sample on how to get local times for different timezones:

const now = new Date();

console.log('Current UTC time: ' + now.toISOString());
console.log('Current Chicago time: ' + now.toLocaleString("en-US", { timeZone: "America/Chicago" }));
console.log('Current Berlin time: ' + now.toLocaleString("de-DE", { timeZone: "Europe/Berlin" }));

The code sample uses the JavaScript Date object and its toLocaleString() function to convert a date into a string in the current locale.

Here is an example of using moment.js, a popular JavaScript library for manipulating dates and times:

const now = moment();
console.log(now.format('MMM DD h:mm A'));

const logAngelesLocalTime = now.clone().tz("America/Los_Angeles");
console.log(logAngelesLocalTime.format());
console.log(logAngelesLocalTime.isDST());

Note that you will need to install the moment-timezone package in order to work with timezones.

What's next