Forum Discussion
JavaScript to Subtract Days from Current Date
Hi Jay. I haven't tested this, but this should give you some ideas:
const currentDate = new Date();
const dateOfBirth = new Date("2005-02-18");
const ageInMilliseconds = currentDate - dateOfBirth; // calculate the age in milliseconds by subtracting the date of birth from the current date
const eighteenYearsInMilliseconds = 18 * 365.25 * 24 * 60 * 60 * 1000; // calculate the number of milliseconds in 18 years, taking into account leap years
const dateOfEighteenthBirthday = new Date(dateOfBirth.getTime() + eighteenYearsInMilliseconds); // calculate the date of the child's 18th birthday by adding 18 years to the date of birth
console.log(dateOfEighteenthBirthday.toLocaleDateString()); // output date of 18th birthday in local date format
Brian,
Thank you for the help. Using what you wrote above, I put this into Storyline:
const currentDate = new Date();
const dateOfBirth = new Date("2005-02-18");
// calculate the age in milliseconds by subtracting the date of birth from the current date
const ageInMilliseconds = currentDate - dateOfBirth;
// calculate the number of milliseconds in 18 years, taking into account leap years
const eighteenYearsInMilliseconds = 18 * 365.25 * 24 * 60 * 60 * 1000;
// calculate the date of the child's 18th birthday by adding 18 years to the date of birth
const dateOfEighteenthBirthday = new Date(dateOfBirth.getTime() + eighteenYearsInMilliseconds);
let player = GetPlayer();
player.SetVar("Brian", dateOfEighteenthBirthday);
And the output in Storyline is this:
Sat Feb 18 2023 07:00:00 GMT-0500 (Eastern Standard Time)
I ran this code multiple times this morning from 0800-0850 and the output has not changed at all. What Math (below) provided appears to be jumping backward like I need. It's last output was:
Sun Feb 27 2005 08:47:00 GMT-0500 (Eastern Standard Time)
The 'Feb 27 2005' portion is exactly what I need, so now I need to figure out how to pull out just that part.
Again, thanks alot for the help!