Hi, I want to retrieve the timezone of the person currently viewing the site. the timezone property in wixsite gets the timezone in the site setting. But I want to get the timezone of the viewer. I want to explicitly display the timezones in which we are displaying timing of the events on the site.
Use getCurrentGeolocation() in the wix-window API to retrieve the user’s IP-based location coordinates. To see how Geolocation works, s ee Ahmad’s excellent Tutorial: Detect Visitors’ Geolocation & IP Address and the Example: Google Places .
Once you have the location coordinates, you can get the time zone using an external service such as TimeZoneDB’s API:
export function tz(lat, lng, timestamp) {
return wixSecretsBackend.getSecret("TimeZoneDB")
.then((secret) => {
let url = `https://api.timezonedb.com/v2.1/get-time-zone? key=${secret}&format=json&by=position&lat=${lat}&lng=${lng}&time=${timestamp}`;
return fetch(url, { method: 'get' }).then((httpResponse) => {
if (httpResponse.ok) {
return httpResponse.json();
}
});
})
.catch((error) => {
console.log(error);
});
}
Note: you will need to sign up for the TimeZoneDB service to get your own API key.
@yisrael-wix , thanks for this answer. I will try out the same.