I have a button that correctly submits todays date to a field named “buttonLastPressed” in a collection when clicked.
now I am trying to use an IF statement to say that IF todays date IS EQUAL TO the date saved when the button was last pressed, then disable the button. (else enable it)
The goal is to have a button that can only be clicked once per day. Thankyou All!
$w("#dynamicDataset").onReady(()=>{
let currentItem=$w("#dynamicDataset").getCurrentItem();
console.log(currentItem);
const todaysDate = new Date();
let LastPress = currentItem.buttonLastPressed;
//disable earn points
if (todaysDate == LastPress) {
$w("#button117").disable()
} else {
$w("#button117").enable()
}
You problem is that JS-dates not only hold the date, but also the time. Actually, it is an amount of milliseconds since Jan 1-st, 1970, UTC+0. That number is then converted into a human readable date on browser level.
You will have to establish for todays date an upper- and lower bound: lower = todays date with hours set to 00:00:000, upper will be 24 hours later (tomorrow). Then you test if the LastPress >= lowerboud && LastPress < upperbound (so basically, if the date is in between 2 dates).
Also, I do not know if this press button is per user or site wide. If you have people coming in from different time zones, you will need to adjust for that.
Thankyou for the reply! That’s such an interesting (and not obvious) thing to know! I’m glad I learned something today!
That’s a bit beyond me - being the newbie that I am - so it seems like I’ll need to spend some time researching, but thankyou for giving me some direction!!
Ita per user - however I’m not too fussed about time zones, or having the button enable at exactly midnight - as long as the user can come back roughly 24+ hours later and see the button re-enables
This seems interesting. The JavaScript date object has several different properties that can help you accomplish this. Here is an easy way to implement something that would work.
You can get the physical day of the date by using the object prototype getDate that returns a numerical value equal to the day of the month.
// get the date like you are in the example code
let currDate = new Date(Date.now())
let day = currDate.getDate()
let lastPressed = new Date(currentItem.buttonLastPressed)
if(lastPressed.getDate() === day) {
$w("#button117").disable()
} else {
$w("#button117").enable()
}
this might be problematic if the button was last pressed a month ago, however if the button would be pressed often then this should not be an issue. If it is then you can also compare months with the .getMonth() prototype and chain that inside the else condition.
Also Also if you wanted to enforce a scenario in which it ONLY re enables after 24 hours has elapsed and not just check that it is a new day (so someone wouldnt be able to press it once at 11:59PM and then again at midnight) you can use the setDate method and then cast the result to a number primitive
let lastPressed = currentItem.buttonLastPressed
lastPressed.setDate(lastPressed.getDate + 1)
let nextEnabledPrimitive = lastPressed[Symbol.toPrimitive]("number")
then you can just get the current date and cast that to a number primitive and ensure that the current number is greater than the nextEnabled primitive
I really appreciate your reply and help! Im certainly not good with velo so I just want to say thankyou again for even providing the code for me!
Unfortunately It didn’t work as I’d hoped - but I think I know why:
I am using this code to add the value to the field named “buttonLastPressed” in my collection when a completely separate button on a completely separate page is pressed
const today = new Date();
$w("#dynamicDataset").setFieldValue("buttonLastPressed", today)
$w("#dynamicDataset").save();
and in the collection it is saving as this format, with month day, year.
Do I need to save it to my collection in the same format displaying only the day rather than its current format? is that what you are referring to when you say "You can get the physical day of the date by using the object prototype getDate that returns a numerical value equal to the day of the month. "
and if so, would the correct code be as simple as adding the “(date.now())” to match the wording it is trying to read from the collection?
const today = new Date(Date.now());
$w("#dynamicDataset").setFieldValue("buttonLastPressed", today)
$w("#dynamicDataset").save();
this makes sense! I’m happy for it to enable 24 hours after if that is the simpler option! I dont quite understand all of what you’re saying but I really appreciate both the advise and code snippets and will give me something to play around with for the next few hours!
you are truely a legend! thankyou so much! do you have a website or social media page where I can write you a review to say thankyou?
and lastly, Do I still use the same code i’m using to add the buttonLastPressed value to my collection in the first place? or do I have to convert that to be a primitive number aswell? this is what im currently using
const today = new Date();
$w("#dynamicDataset").setFieldValue("buttonLastPressed", today)
$w("#dynamicDataset").save();