How to get user's timezone

Anyone knows how to get the user’s timezone?

I need it so I can schedule some jobs and reminders according to the user’s timezone location. I can ask users to manually enter their timezone but I would rather not do that extra step for users and just get their timezone automatically from their phones.

Also I was hoping to find a more local solution that doesn’t need an api call to a world clock api.

Thnaks in advanced!

I’m not sure but it occurred to me that you could get the device hour from Thunkable and then compare it to GMT via an API call and then based on the difference, figure out their timezone. It wouldn’t be perfect, though, because for example here in California we have Daylight Savings which alters the time by an hour.

1 Like

Hey thanks for the suggestion. That’s exactly what I’m doing now but it’s kinda messy Or prone to bugs due to daylight saving time. Also I was hoping to find a more local solution that doesn’t need an api call to a world clock api.

Maybe you can get the user’s coordinates using the Location Sensor component, and then put them in this API, and Google will send back the timezone.

2 Likes

Hi there,

I would also recommend using an API! You can check out this tutorial for my Reverse Geocoding App - Get Location from Latitude/Longitude that uses the OpenCageData API. This tutorial will return the user’s country, but you can also get the user’s timezone.

This will be in the format:

"timezone" : {
               "name" : "America/Sao_Paulo",
               "now_in_dst" : 0,
               "offset_sec" : -10800,
               "offset_string" : "-0300",
               "short_name" : "BRT"
            },

Here is their description of the timezone property:

contains the keys name , now_in_dst , offset_sec , offset_string , short_name.
When the coordinates are in a country the name will be of the form Continent/City, the format used by tz database on *nix systems, for example Europe/Lisbon. When the coordinates are not in a country (for example in an ocean) the format will be an offset to/from GMT, for example GMT+2.
Learn more about timezones and how they are typically represented on *nix based systems over on Wikipedia.

I got this information from this web page: OpenCage Geocoding API Documentation

Hope that helps!

6 Likes

Thanks for the suggestion guys!

1 Like

I found the easiest way to get the timezone is by using the date() object of the vanilla JS and all what you need is the web viewer instead of web API calls.

Get the number of seconds since 1970 and multiply it by a 1000 and create a new date object from it.

Then use the newDateObject.toLocaleString(“en-US”, {timeZoneName: “short”})

This will display the time zone along with the date in a human readable format.

2 Likes

Hi, it sounds good could you explain it further? How do you use javascript in Thunkable? Thanks

1 Like

I am not good with APIs or databases. However, I don’t think there is any way you could use javascript in thunkable.

1 Like

YES, there is a way to use JavaScript in Thunkable. This is done using the Data Viewer extension to receive data from Thunkable and to send data to Thunkable.

Web Viewer Extension Example

This should show you how Thunkable interact with web pages.

3 Likes

Sorry I am confused should one use the Data or Web viewer? Where should one upload the javascript file?

@a.sa.pellaiahj3c & @humanoid APIs are definitely a big topic and can be tricky to understand if it’s your first time seeing them. Thankfully there are lots of great tutorials, both here and on YouTube, that explain this topic.

Here’s a great one from @tatiang to get you started:

2 Likes

Okay, I’ll check it out!

1 Like

@a.sa.pellaiahj3c
@humanoid
@domhnallohanlon
Please see this example of using JavaScript file to get the Date and Time and the Time Zone.
https://x.thunkable.com/projectPage/6093d4a8b86e4b01ee61b244

This is the asset file in the project
timezone.txt (394 Bytes)

1 Like