Reset input by date and/or time

Is there a way to reset input fields at start of next day (midnight)?

Basically if the user came to the page, updated some numerical fields, closed out and returned later, the last entry would remain until the start of the next day.

Thanks in advance!

image

Yeah. You can set hours minutes seconds to zero. You may have to do the comparison “backward” but you can do that. Anything else?

Thanks Keith. I am not exactly sure how I would set that up (fairly novice). Any insight on how I might attempt to reset the field at a specific time (what steps to take)?

Thanks so much!

You know, I probably misunderstood the original question. I see that what you are trying to do is retain a certain value, but only for a certain duration, correct?

Since you need to retain these values between sessions, you need to write them to the database. The correct way to do this is to create a new data type (a new Thing as Bubble calls them) to store the values of these inputs and put a thing of that data type on the Current User.

So I’m gonna create this, a type called Foobar, to hold a Foo and a Bar, like so:

And now we want a field of type Foobar on the User object:



Now when your inputs for Foo or Bar are changed, we write those values to the database by changing Current User’s Last Selected Foobar’s Foo or Current User’s Last Selected Foobar’s Bar. We can do this with a workflow action or via autobinding.

Here’s the interface I built for this example – I’m using 2 sliders for Foo and Bar:

Since you seem to have a custom input thingy you’ve built, you probably have events that get triggered, so for my example, we’ll not use autobinding, but make these changes when the inputs are changed:


Now, one reason we made a new data type for our Foobar (which, again, is just a place for us to store the values of these inputs) is that all things have a Modified Date, which is updated whenever the value is changed. So we don’t have to track that separately. Neato, right?

We can add a text element to our page that we’ll use as debug info and so we can see what’s going on. More people should do this as they are building new features and – if they did – we’d see a lot fewer silly questions here in the forum! (I am not implying this question itself is silly – it’s actually rather interesting). Here’s what I made:

And, in run mode (Preview), we can fiddle with the sliders and see what happens:

Hmm. That’s not right, is it? What have we not done yet?

Well, we have not yet created a Foobar object and attached it to the User. This is why we see no values for the Foobar field (the User’s “Last Selected Foo” as I called it).

This will be part of our initialization step for the page. We will build a workflow that triggers on page load that:

  • Creates a Foobar and attaches it to the User (if the User does not yet have one) and
  • Checks when the Foobar was last modified.
  • If the current date is not the same as the modified date, we will reset the Foobar values to defaults.

So we’ll build that next. But I am currently away from my machine, so I’ll continue this later…

“To be continued…” Continued at: Reset input by date and/or time - #7 by keith

I believe I am following along with each step and they seem on point. I look forward to seeing how you complete the next steps by checking the last modified date and if not the same to reset the values.

You are correct my custom input can be triggered in 3 different ways, clicked the up or down buttons or typing the value right into the input. Currently I have these workflows setup to save to the database. Is there a way for me to share the application with you to show you how it is currently setup?

Being a novice, it is greatly appreciated to have a lending hand with the proper structure of the database and why to set it up a certain way. I am not sure I completely understand yet in your above example of why to write the data from foobar to a field of foobar under the user and hoping to learn from this example to get a better understanding of data structures. I learn well by watching videos and then doing. Any recommendation you might have on videos to learn about setting up data structures in different scenarios?

Thanks again Keith! Looking forward to seeing how this ties together!

1 Like

continuing now…

Continued from Reset input by date and/or time - #4 by keith…

So, we must now create an initialization step for the page to handle the three things I mentioned previously.

First, let’s create a Foobar for the User if none exists:

^^^ The Foobar has not been created if the User’s Foobar does not exist, see? So we create one only if it does not exist (User’s Foobar is empty/null).

Now we attach the newly created Foobar to the User (again, only if we just created it – the condition for that is if the result of the first step is not empty):

Now any User on this page has a Foobar. We can proceed to check when the Foobar was last updated. There are several approaches we might take at this point that are worth describing.

In your use case, @jasonturo, you desire to know: Was the Foobar diddled today? If so, we want to retain the value. If the Foobar was diddled on a day that is not today, you want to disregard whatever setting the User previously selected.

We could do such a comparison the “proper” way, using date math. (When I say, “date-wise” this is what I mean.) In that case, we would do something like:

  • compare the current date/time to the Foobar’s Modified date. Is the current date/time within the range of: the start of the Modified date to the end of the Modified date? The start of the Modified date is: Foobar’s Modified date:change hours and minutes and seconds to zero. The end of the Modified date is: Foobar’s Modified date: change hours to 23, change minutes to 59, change seconds to 59. … Do I even need to continue? WHAT A HASSLE! Let’s do something else…

I put the above bullet here just for completeness, you can check if Current Date/Time is within the range defined by the start of Modified Date to the end of Modified Date, but this is actually overkill. We can instead simply compare the dates text-wise.

Here’s the thing: Let’s think about Current Date/Time. Current Date/Time is “now” and it looks like this if we format it as MM/DD/YY:


Run mode:

See that? (All the yellow shit – err, stuff – in the above picture?) That’s how we know if a some date is “today” using text-wise representations.) If Current Date/Time:formatted as MM/DD/YY is the same as some other date/time:formatted as MM/DD/YY, those date/times happened on the same day (as we would say as normal human beings).

^^^ this is the short answer to your original question, BTW.

So that’s the quick and dirty way to tell of two date objects share the same date part. (For a lot more on the complex nitty gritty of date/time objects, search for @keith date/time here in the Forums.)

The important thing to understand is that this is a bit of a hack, if you will. We generally want to compare date/times datewise, but figuring out if two things are happened “today” is a special case and we can use this (very performant and much simpler) textwise comparison hack to determine that.

Armed with this knowledge, we can proceed to create our initialization workflow: The third step in our workflow is to set the Current User’s Foobar preferences to a default… BUT ONLY IF they were modified on a day that is not today. Like so:

^^^ If the Foobar’s modified date:formatted as MM/DD/YY is not the same as Current date/time:formatted as MM/DD/YY, these are different dates (and so we should reinitialize the values). If they ARE the same, the Foobar diddling happened sometime earlier today and we should respect the existing values.

I did not yet fill in the values for Foo and Bar. Your defaults might be different (they might even be variable and thus be expressions!), and so you’d put something there. However, in my case, I’m just going to set them to empty (by leaving the values empty). This is analogous to the case where the Foobar is just created and those values would be empty.

I tweaked my debug text a bit, too, so we can observe things that are happening:

OK! So now we can go into run mode and diddle our Foo and Bar sliders (don’t get too excited). We will see that a Foobar actually exists and that its values are being changed:

One final thing: when we reload the page, what happens? Do our Foo and Bar sliders respect the preset values? Let’s check. I hit refresh and find this:

Whoopsie! We are not setting the initial value for our sliders to be the value of Current User’s Foo and Bar. So let’s do that:


That being done, we should see things behaving properly on reload:

Hooray! Done!

(Now, you might be wondering: Will the Foobar’s Foo and Bar really be reset to null after midnight? It’s hard to test, isn’t it? But trust me, they will. But you can just stay up until midnight diddling your Foo and Bar until the clock turns tomorrow if you choose.)

You can examine the lil project I built for this example here (anyone can view). Edit mode:

Run mode:

2 Likes