Date Calculation

Coach enters player name and age range.
Coach fills out the data entry form (below) with player name, date pitched and number of pitches.

  • How would I calculate the rest days for the player based on the # of pitches and the age range (see rules)
  • And, how do I calculate the next available pitch date (calendar) based on the same pitch rules (see rules)


RULES DATABASE

Following

Hi,

To begin, you should create a new collection with the player names and their birth dates.
Next, change your Rules database as follows - split the minimum and maximum ages to two separate columns. Do the same for the pitch range.
You should end up with the following columns:
Minimum age, Maximum age, minimum pitch range, maximum pitch range.

Next, create a function that will get the current player’s age according to the player database.
I created basic code example that should get you started.

export function dropdown1_change(event, $w) { //Runs every time a change was made to the dropdown box
	console.log(event.target.value)
	wixData.query("dasd")
		.eq("title", event.target.value)
		.find()
		.then((results) => {
			let firstItem = results.items[0];
			//gets date of birth in a millisecond format
			let playerDob = firstItem.dob;
			//calculates the age and changes from ms to years
			let playerAge = ((Date.now() - playerDob) /1000/60/60/24/365);
			//displays the result
			$w('#age').text = "Age: " + playerAge;
		})
		.catch((err) => {
			let errorMsg = err;
		});
}

Now that you have the player’s age ready, you can query the rules database once the data is submitted according to the selected fields per the example below:

export function submit_click(event, $w) { //user clicked the submit button
	wixData.query("rulesDatabase")
		.gt("minAge", $w('#age').text) 
		.le("maxAge", $w('#age').text)
		.gt("minPitchRange", $w('#pitchRange').value) 
		.le("maxPitchRange", $w('#pitchRange').value)
		.find()
		.then((results) => {
			let rule = results.items[0]; //located the matching rule
			//you should now have the number of rest days the player should have, use wixData insert to add this to a collection
		})
		.catch((err) => {
			let errorMsg = err;
		});
}

Good luck!

Beautiful :slight_smile: Good stuff, Ido :slight_smile:

So Ido ---- I couldn’t tell in the code above …

If we want this to happen:

Jan 1, 2018 + 4 (days of rest) = Jan 6, 2018 is the new eligible day to pitch

How do we get THAT from the calculations above? (So we can display the new eligible Date — not the # 4 days of rest)

Thanks Ido and Nayeli! The 4 days rest is one field we need, but as Nayeli notes, we also need the actual eligibility date to pitch. So both calcs, actual number days rest AND the next date eligible. Thank you very much!

Hello Mark and Nayeli,

Calculating the next pitch date is pretty straightforward:

//...

//for example the number of rest days is 4
const restDays = 4;
const dayInMs = 1000 * 60 * 60 * 24;
const nextPitchDay = (restDays * dayInMs) + Date.now(); // Wed Jan 17 2018 17:14:34 GMT+0200 (IST)
//Do something with nextPitchDay
//...

Thanks for sharing this Ido.