Hi team, it has come to my attention that the below code has stopped working as intended, I’m positive I haven’t changed anything that should effect this but I may have missed something.
Basically if it is 30 days before the start date then it will change the options of the drop down for the deposit but the code runs even if the date is more than 30 days.
var currentDate = new Date();
var currentTime = currentDate.getTime();
var timeThirtyDaysHence = currentTime + (30 * 86400000);//86400000 milliseconds in a day
var thirtyDaysHence = new Date(timeThirtyDaysHence);
$w.onReady(function () {
console.log(thirtyDaysHence)
console.log(currentDate)
if (thirtyDaysHence > currentDate) {
$w("#deposit").options = [
{ "label": "50%", "value": "0.5" },
{ "label": "100%", "value": "1" },
];
}
});
Thank you!
Hi Stephen,
The code is missing a parenthesis on the last line.
Instead of
};
It should be
});
Having said that, I’m having trouble understanding the logical purpose of the code. ThirtyDaysHence is always going to be greater than the current date. You referenced the start date. It seems that you should be using that in your comparison with the thirtyDaysHence instead of currentDate.
Hi Anthony, thanks for helping. I had just copied the code and left out the remaining code in the onReady so this is just a typo on the forum. there are no errors. The code was working perfectly fine until I noticed a few days back that it was always functioning, regardless of the date being less than 30 days.
The idea is that when someone goes to book, if the date is less than 30 days prior to the start date i.e.
Booking date 4th Jan
Start date 12th Jan
then it would display only an option to pay 50% or 100% of the course.
Normally there are 3 options
25%
50%
100%
Anthony is right: you do something like this: A=1, B=A+30. If B > A, …
B will always be greater then A, so this can never have worked. What you need to do is compare book date and start date and then do something if difference between the two > 30. Below a copy of a datediff-function I found on StackOverflow:
function datediff(first, second) {
// Take the difference between the dates and divide by milliseconds per day.
// Round to nearest whole number to deal with DST.
// If you call it with Now, Yesterday, it returns -1. The abs takes care of ... returning the abs (1 instead of -1)
return Math.abs(Math.round((second - first) / (1000 * 60 * 60 * 24)));
}
Hope this helps.
@giri-zano thanks for your help. I noticed this in the code yesterday, and I think I found where my mistake was in terms of thinking that the code worked. I had the same code inside a repeater and use a static date instead of ‘new date’
If I’m honest it will take me a fair bit of research to get my head around this but thanks for pointing me in the right direction 