JavaScript to Subtract Days from Current Date

Feb 16, 2023

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
Brian Batt
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

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
Math Notermans

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 new Date 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 the console.log() method.

Jürgen Schoenemeyer

here is a javascript example

const years = -18; // 18 years back
const days = +10; // 10 day forward

const event = new Date();
event.setFullYear(event.getFullYear() + years);
event.setDate(event.getDate() + days)

const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};
const result = event.toLocaleDateString('en-US', options);

// 'en-US' -> "Saturday, February 26, 2005"
// 'en-GB' -> "Saturday, 26 February 2005"
// 'de-DE' -> "Samstag, 26. Februar 2005"

more info for Date and toLocaleDateString

Jay Cooper

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!

Jay Cooper

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!

Jürgen Schoenemeyer

you have nothing to adjust - the javascript library does all adjustments automaticly,
this is modern JavaScript

const years = -18; // 18 years back
const days = +10; // 10 day forward

const options = {
weekday: 'long',
year: 'numeric',
month: 'long',
day: 'numeric'
};

const event = new Date( 'December 28, 2023 23:15:30');
console.log( "1 - now", event.toLocaleDateString('en-US', options) )

event.setFullYear(event.getFullYear() + years);
console.log( "2 - year", years, event.toLocaleDateString('en-US', options) )

event.setDate(event.getDate() + days)
console.log( "3 - days", days, event.toLocaleDateString('en-US', options) )

result:

  • "1 - now", "Thursday, December 28, 2023"
  • "2 - year", -18, "Wednesday, December 28, 2005"
  • "3 - days", 10, "Saturday, January 7, 2006"

or with February 29

const event = new Date( 'February 29, 2024 23:15:30');
console.log( "1 - now", event.toLocaleDateString('en-US', options) )

event.setFullYear(event.getFullYear() + years);
console.log( "2 - year", years, event.toLocaleDateString('en-US', options) )

event.setDate(event.getDate() + days)
console.log( "3 - days", days, event.toLocaleDateString('en-US', options) )

result:

  • "1 - now", "Thursday, February 29, 2024"
  • "2 - year", -18, "Wednesday, March 1, 2006"
  • "3 - days", 10, "Saturday, March 11, 2006"


    or the same date, but -16 years
  • "1 - now", "Thursday, February 29, 2024"
  • "2 - year", -16, "Friday, February 29, 2008"
  • "3 - days", 10, "Monday, March 10, 2008"