Working with time

I’ve posted a similar topic last time but couldn’t work it out.
Sorry for the repost.
My use case is this : Shops register with us, we will display those shops to users and they order whatever they want from those shops. I’m taking Open time and Close time from shops. That open and close time are being saved as Dates in Data. Now I need to display only shops that are currently OPEN to users. While doing a search for shops, all I’m getting is “Open time” and “Closed time” which are basically dates and not times and I don’t seem to find a way to format them in search functionality. How can I compare those dates with current time?
Please help

1 Like

That would lead to a lot of extra workflow operations and if the app were to scale and have 1000s or 100s of 1000s of shops that is A LOT of extra workflow operations happening twice daily.

Need a way to manipulate the dates from the database as a comparison to current date time to change the value of the text field display.

Learn More

Boston85719 is an expert Bubbler with a decade experience as an educator. Real name Matthew, he has been actively building SaaS apps, marketplace apps, scheduling apps and more for clients, himself and for sale as templates.

As an official Bubble Bootcamp Instructor, he leads Bubble Bootcamps on a regular basis.

Always willing to offer advice via the Bubble Forum, Matthew also offers Private Personal and Group Training Sessions.

Through his site, NoCodeTrainer, Matthew provides a range of tutorials with editor access to help you jumpstart your Bubble development.

Always accessible you can send Matthew a private message via the forum or send an email directly with your requests.

Be sure to checkout the Stripe Integration Course when you are ready to integrate Stripe payments to start monetizing your application via product sales or subscriptions.

Stripe Integration Course

NoCodeTrainer.com

1 Like

You can use the command :extract to extract the hours of the stores and compare it to the Current Date/Time:Extract Hours

This should work.

1 Like

I don’t find a way to apply any filters while searching data. Is it possible?

Great idea actually. But we don’t want shops to manually mark them open. There should be an automatic way to do this…

When you do a search for data you can add constraints in the panel that opens to the left, which allows you to filter the data. Additionally, if you are accessing data from, let’s say elsewhere on the page, you can use :filter to access this same panel to add constraints! Let me know if you have any issues or questions :slight_smile:

While filtering the data, all I’m getting are the fields from the datatype I’m searching in. I’m selecting “Open time” to compare it with the current time
But I really can’t do anything to the “Open time”, it is what it is. I can’t seem to extract hours and minutes or whatever. After selecting “Open time”, all I’m getting are mathematical operators urghhhh

No. While they’re registering with us, we will be taking their open and close times. Assume they open their shop at 8am and closes at 10pm. Users from that area will see this shop in the marketplace only in between 8am to 10pm. I’m using search to find shops in the user’s current city, but I can’t seem to find a way to filter shops by their open and close time.

The answer is pretty simple. you need to change time into minutes. 60 minutes in an hour 24 hours in a day equals something like 1400 minutes so if I open at 8 AM I open at minute 480. When you want to do filtering you could save this value as a field on the businesses.

Then instead of filtering by time which is you can’t do for open and close times for the day you filter by the minute. So if I want to find businesses that are open at 8 AM I do a search for any business who start time is equal to or greater than 480

You create a numeric range. Start minute - end minute

8 AM - 12 PM
Is

480 - 720

1 Like

This sounds like the trick. I’ll give it a try very soon
Thank you so much man

No worries mate. Thank you so much for your time. Have a great day!

1 Like

Take a look here:

2 Likes

This is exactly what I needed. Thank you so much man! You saved my day

2 Likes

@rpetribu and @jared.gibb does this approach you are implementing with the number of minutes approach work when viewing the page from a different timezone?

My first thought on this topic is what @rpetribu had first suggested with a manipulation of the date in a conditional.

I had to do this recently using date ranges for the opening and closing times as the app was setup to allow for stores to keep their hours in a more dynamic way to accommodate for holidays and such. And it was a real pain and wondering if I over complicated it and the number approach will account for the timezone.

Not really a performance issue from a user perspective until the capacity gets maxed out…not saying the capacity would get maxed out when performing this type of backend action on a single value, but at scale it can eat into capacity and reduce performance for users on the site as the capacity usage increases.

Basically, my opinion is that reducing workflows is always better, as no matter how small the capacity usage may be, every little bit counts towards optimizing performance.

My original assumption though was that users would be opening their stores at random specified times and closing at random specified times, similar to how a scheduled auction would work. Which is why I was thinking of the scheduled API workflows, so that a store could be tagged as open or closed automatically, and wouldn’t have to worry about doing any math or taking into account time zones. The only required constraint would be open = yes. Scheduled API workflows seem to be designed exactly for this use case, as per the documentation. Am I missing something though? Are scheduled API workflows hard on the capacity of the app? I could see if you were constantly running complex workflows or running a ton of workflows all scheduled for the exact same time, but if it can be assumed that the workflows will be scheduled reasonably spread out then it shouldn’t be a problem. Do you agree or disagree? (Of course scheduled workflows for this actual use-case wouldn’t make sense, I’m just curious about your opinion on scheduled API workflows)

Hi @boston85719 !

This is complicated :sweat_smile:

Assuming that this is going to be a static number (14:00 = 840) registered in the database, it will always look at the users (site visitors) timezone. So if the store closes at 14:00, everyone who is visiting the site at night, will see the information that the store is closed. What is not exactly thru…

So the answer is no, it will not work for people in different timezones.

What you can do (and this is what I do in my own app) is to use a little bit of javascript to compensate the difference between both timezones. I am from Brazil, our timezone here is -3.

So using the code below I can compensate this difference and adjust all my calculations using the Toolbox plugin.

var x = new Date();
var br = -3; //My timezone (in hours);
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
var adjust = (br+currentTimeZoneOffsetInHours)*-1
alert ("Compensate: " + adjust );

In the code above, the variable “adjust” is how much I need to shift user’s timezone to match my timezone. So everything works fine :wink:

You can try this code at:
https://www.webtoolkitonline.com/javascript-tester.html

If you want more details about it, just send me a PM :+1:

2 Likes

Backend workflows are just as hard on capacity as any workflow on the page may be. It is all dependent on the workflow actions and how much data processing may be taking place.

You are correct in that the single field change would not be significant as a one off. What I was pointing out was a consideration for scaling an application. My preference is to build with scalability in mind, and if I had a successful app with 100K store I wouldn’t want to run 200K backend workflows every day to show a closed/open text.

As mentioned in previous reply, my opinion is to reduce the need for workflows no matter if they are not expected to be capacity intensive, if I have another way to reach the same goal. In this case, to show a closed/open text I would rather manipulate the data already on the page and not need to do a single workflow for that.

This is awesome. Thanks for sharing that.

I’m not a coder so coming up with the javascript, or even thinking to look for something like it wouldn’t come to mind.

I’ve done something similar in some of my apps to compensate when showing dates returned from APIs which would need to be adjusted by a number of minutes for the ‘UTC offset’.

I’m going to try and implement the javascript approach and see how I get on with it.

I may have to take you up on that if I can’t get it sorted.

One question about the script you posted. The value of -3 is the number of hours offset for brazil and the use of br is just a ‘label’ so I could easily replace br with a label I can remember like ‘tz’ and in the var adjust start the formula with ‘tz’?

Just thinking of how to make it so I could do it dynamically before trying to jump into it.

Glad it helps! :grinning:

Yes yes… :+1:
br is just a variable. You can change it for whatever you want.
Just remember to change it in the line below too :point_down:

var adjust = (br+currentTimeZoneOffsetInHours)*-1

1 Like