JavaScript to Subtract Days from Current Date
Feb 16, 2023
By
Jay Cooper
I'm working on a course that involves sorting juveniles with one of the criteria being their age. I'm looking to display each child's age based on the current date, minus X number of days so that the child's birthday the relative no matter when a learner takes the course.
For example, I need a child that is a couple days short of 18 so I'm looking for JavaScript that will calculate the current date minus X number of days to produce a date of birth that would make the child a few days short of 18 (e.g., Feb 16, 2023 minus 6564 days).
I've used JS in my projects before (I'm novice to low intermediate), but this one has me stumped.
Thanks in advance for any help.
Jay
7 Replies
Hi Jay. I haven't tested this, but this should give you some ideas:
Try chatGPT...
I entered...
Javascript to calculate current date minus 18 days
The result was this...
// Get the current date
const currentDate = new Date();
// Subtract 18 days from the current date
const eighteenDaysAgo = new Date(currentDate.getTime() - (18 * 24 * 60 * 60 * 1000));
// Format the date as a string
const formattedDate = eighteenDaysAgo.toISOString().substr(0, 10);
// Output the result
console.log(formattedDate);
With this extra info... for these specific cases chatGPT is great...
This code first creates a
Date
object representing the current date and time. It then subtracts 18 days from this date by creating a newDate
object with a timestamp that is 18 days earlier. The resulting date is then formatted as a string in the format "YYYY-MM-DD", which is commonly used for dates in web applications. Finally, the result is output to the console using theconsole.log()
method.here is a javascript example
more info for Date and toLocaleDateString
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!
Math,
This looks like what I needed! Thank you very much. I input this to Storyline:
const currentDate = new Date();
// Subtract 18 days from the current date
const eighteenDaysAgo = new Date(currentDate.getTime() - (6564 * 24 * 60 * 60 * 1000));
// Format the date as a string
const formattedDate = eighteenDaysAgo.toISOString().substr(0, 10);
let player = GetPlayer();
player.SetVar("DateValue", eighteenDaysAgo);
player.SetVar("Math", formattedDate);
The output then is:
DateValue = Sun Feb 27 2005 08:47:00 GMT-0500 (Eastern Standard Time)
Math = 2005-02-27
The 'Feb 27 2005' portion of the output is exactly what I need, but I can work with the '2005-02-27'. Either way, I need to remember/figure out how to extract just the portion I need.
Thank you very much for your help!
Jurgen,
Thank you for the help. The problem with breaking the new Date up this way is how to make adjustments if the date is at the beginning of the month or year.
I appreciate your help.
v/r
Jay
you have nothing to adjust - the javascript library does all adjustments automaticly,
this is modern JavaScript
result:
or with February 29
result:
or the same date, but -16 years