Forum Discussion
JavaScript to Subtract Days from Current Date
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.
- JayCooper3 years agoCommunity Member
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!