Get the GMT offset for a Timezone

Anyone know if there a way native to Bubble to get the hours/minutes offset for a timezone?

For example: America/New_York = -5 or -4 depending on daylight savings.

Hey @kramwe,

A timestamp comparison between a User and your server will do it. We recently built out a scheduling system for a promising platform and had to address this issue when webhooks (and therefore, GMT) came into play. If you have the user experience to collect the timezone from a user, typically in the onboarding and/or account management pages, it’ll be easier to capture the difference and revisit for any number of use cases. :slight_smile:

Okay. So something like Current Time in UTC - Current Time in User TZ? Do you have an example? Formatting in Bubble converts the date to a text field, so I’d like to see how you got around that.

BTW - if you’re storing the offset, how are you dealing with daylight savings? I ask b/c, we’re exploring an email service that stores the offset rather than the timezone (eg. America/New_York) and it’s not clear to me how/if they handle daylight savings.

Exactly, making sure that your timestamp unit reference is always the same; we prefer milliseconds to take advantage of Bubble’s :extract module whenever needed. An external call to verify daylight savings should do the trick, and the rest is the same. Most scheduling services that require a timestamp go straight for GMT, the offset should be something you can look up and confirm in their documentation. :grinning:

I still don’t understand how you did it. How do you create a date field in Bubble in the user’s timezone and in UTC / server time? The format function converts the timestamps to text fields which cannot be subtracted.

You should be using the :extract module to grab the UNIX timestamp, which is a number. :+1:

Okay, but how do I make one of the timestamps in user’s time and the other in UTC?

POST to one of your API Workflows, which forces Bubble to default to GMT/UTC/server time. :slight_smile:

Post the user’s current date/time to an API workflow then in API workflow in action take user’s current date/time - current date/time (this will be in UTC since it’s server side). Correct?

We had an external service hitting the API Endpoint directly, which did give us the server time needed. I don’t believe making an HTTP request to the endpoint will render the same results as if you were to Schedule an API Workflow, as Bubble may pass the User’s own timestamp information along which should be confirmed. :thinking:

1 Like

I think this is the case. I recall us at one point trying to solve for users who were resetting their computer time 5-10min in future. I tried using the API workflow to calculate the offset, but it gave me back the user’s computer time. I’ll spring for an external call on this one.

Our thinking as well. Once you have the request come in, save Current date/time’s timestamp using the :extract module and compare it to the timestamp you picked up client-side; that’ll be your offset for future reference. :slight_smile:

I’m wrangling with this issue now. Would love to know, how did you set up an external API call?

Last word was the Bubble team was going to expose the offset. I’d first check with them if it’s a thing now.

The JS toolkit + some custom JS will get you the offset as well. Keep in mind, that if you’re storing the offset, you need to consider daylight savings.

Right, my issue though isn’t about getting the offset. I want to save a date from a date/time picker in a specific timezone, not the users browser timezone.

So for example, I’m in CET (UTC +2), but I want to save a time of 12:00 in GST (UTC +4). I thought I could pass the date through an external api call to force bubble to see it as UTC time, and then apply the offset to save it in the specified timezone (GST in this example). So that’s what I’m wondering how to do.

But I think even that will give me issues, as the UTC time, when converted to my specific timezone, will give me the UTC + offsetted time (12:00 + 4 = 16:00).

So what I’m really wondering is if there’s a method to simply ‘reassign’ the timezone (say from UTC to GST), without actually converting the time between one zone and another?

We do something similar for our event time picker.

User wants to schedule an event @ X time in Y timezone. I built a blockspring to do this (happy to share the code), but Gaby has a better solution (Open Block for Converting Timezones). Blockspring has frequent outages so was unreliable; we’ve since been using AWS lambda which is never out and is more flexible but has setup costs. Check out Gaby’s plugin. Does that get you what you want?

Thanks for the tips @kramwe . I managed to get something going that converted the date/time of an input into UNIX (with this plugin), then converted that from a specified timezone into a UTC UNIX with Gaby’s timezoneDB plugin, then converted that UNIX to a readable date by adding the UTC UNIX as seconds to 1st Jan, 1970 at 00:00:00 (as in this post).

It’s a mismatch of things mentioned in all the various threads on timezones.

This final UTC output is actually +1 hour off for reasons I don’t understand, so I subtracted 1 hour from the final UTC time and it works (if someone could explain why I’ll sleep easy tonight). image

Here is the link to the page for other people’s reference.

1 Like

@romanmg - ^ gaby do you know if your timezone converter plugin handles daylight savings?