[Solved] Can I enable a button at 5:00am every day?

Hello everyone.

The title basically sums up the issue that I’m attempting to address. I’ve been playing around with some of the features but can’t quite figure out how to design a function that will allow my button to be disabled after pressed, but become available again at ex. 5am. The idea is that this button can only be pressed once per day. I appreciate any insight as I couldn’t solve this by searching the forum myself and I’m sure this will help others as well :slight_smile:

You can use the following blocks
image
It should Work

It looks like this can be circumvented by simply closing a re-opening the app. Unless there’s a way to have this running in the background that I’m not aware of?

Use a stored variable to keep track of when the user clicked the button last?

@anush_bh20910 That will allow the button to be clicked if and only if the hour is 5. But I think @earthquack28ur actually wants the button enabled at 5am and after that. So for example, if I pressed the button at 8pm, I couldn’t press it again until 5am the next day. But I could press it at 6am or 10am or 1pm or…

There’s no way to run code in the background in a Thunkable app. But you can definitely check the time each time the screen is opened and as @catsarisky suggested, save the current state of the button (as a variable value) in a stored variable.

@tatiang is correct. I’ve been trying to come up with a function that tests the current time as well as the seconds since 1970 and comparing it to the last stored variable to indicate that it is after 5am of the following day. I just can’t quite figure out how to format the function to do so. I like the idea of storing that as a variable so that it can be compared every time the app is opened. Ultimately I’m going to need a similar function to process once a week, but that will be far more complex.

What about something like :

When button is clicked
   If device month ≥ stored month AND device day > stored day AND device hour ≥ 5 
      then 
        [do whatever actions you want]
        set stored month to current month
        set stored day to current day

It would be trickier to enable/disable the button because you’d have to check the date & time constantly or at least every so often.

Also, my suggestion probably needs adjusting. For example, if you get the month and it’s 12 and then the next month is 1, my If condition isn’t going to work correctly. Same with end of month days like April 30 to May 1.

So maybe comparing “seconds since 1970” to a stored value and a combination of the above?

This is basically what I’ve come up with. I’ve done some testing by adjusting my phone time ahead and it appears to work. My guess is that I can do the same thing for the weekly function by converting the day of the week to a time and adding it to the equation.

Thank you everyone for your help :slight_smile:

2 Likes

We really need blocks intended to work with time. Like, blocks based on the moment.js or luxon.js libraries

Any ideas if this will ever happen @domhnallohanlon ? Using the seconds since 1970 block is useful, but parsing a human readable timestamp into seconds and vice versa is not easy with thunkable. I will be at this is the most effective way to work with time in my opinion.

1 Like

It’s not on the roadmap at the moment unfortunately @jared…but never say never!

I’m not familiar with either of these - can you share a couple of examples of how they make it easier to work with dates and times?

1 Like

Sure thing!

Here’s a thread on it! Essentially the benefit here would be the ability to turn second since 1970 into a human readable timestamp and vice versa, turn a human readable timestamp into a second since 1970. That makes it a whole lot easier to do date math. It’s a whole ID easier to add 86,400 seconds to the previous number than it is one calendar day and to figure out manually with the next calendar day and month are going to be. Currently I uncle user has to figure those things out manually and that’s just not necessary currently with the given JavaScript architecture that’s available.

In addition it allows you to return a wide range of human readable timestamps from the second since 1970. It also allows you to easily parse it into any time zone you need it to be in. This comes in handy when you’re doing things like Making a scheduling app

const moment = require('moment');

const timestamp = 1519482900000;
const formatted = moment(timestamp).format('L');

console.log(formatted); // "02/24/2018"
2 Likes

Why would you want to link any library to use a function that is natively in JS.

You can use something like this. Remember JS uses the time upto the millisecond so you need to multiply the data number by 1000 to use it in JS and then use the date() function to get the required date format.

const unixTimestamp = 1575909015

const milliseconds = 1575909015 * 1000 // 1575909015000

const myDate = new Date(milliseconds)

const humanReadableDateFormat = myDate.toLocaleString() //2019-12-9 10:30:15

The date object has other functions and the .toLocaleString() has other parameters to format the date in different ways.

1 Like

Yes! I’ve been doing calendar work in bubble and utilizing the libraries! Thanks for chiming in @muneer

Still, I’d love to see some blocks to work with dates.

1 Like

Yes @jared having date/time blocks in Thunkable is essential. Reaching out to web viewer and back is not a solution and has its own drawbacks but it is now a workaround other than relying on APIs till the blocks are available.

1 Like