Using Javascript for X Days Ago in MM/DD/YYYY Format
Oct 04, 2023
Hello,
I would very much appreciate some help here. I need to have the learner open up this learning and have the current date displayed, as well as some dates prior to it (-10 days, -15 days, -2 months) in MM/DD/YYYY format.
For example,
-
- Today is: 10/04/2023
- Document Uploaded: 10/02/2023
- Case Started: 09/14/2023
I found this code for setting the start date:
let currentTime = new Date();
let month = currentTime.getMonth() + 1; let day = currentTime.getDate(); let year = currentTime.getFullYear();
// Putting it together
let dateString = month + "/" + day + "/" + year;
//Pushing data to Storyline
let player = GetPlayer(); player.SetVar("todaysDate", dateString); player.SetVar("month", month);
And I found this code for adjusting the dates:
var date1 = new Date();
var daysToMove = -90;
date1.setDate(date1.getDate() + daysToMove);
var player = GetPlayer();
player.SetVar("90daysPrior",date1.toDateString().replace(/^\S+\s/,'') );
The first one gets me the current date and in the right format but I can't subtract the days. The second one moves the dates but isn't in the right date format. Any guidance?
7 Replies
here is an example to add/subtract days/months/years from the current date
result:
https://360.articulate.com/review/content/773e8a5c-fbb4-41d3-b2cd-0efb109cfa9f/review
Thank you!
Solution:
For displaying today's date:
Solution
For subtracting days:
Thank you, Maddie, for posting your follow up! This is just the solution I was looking for to display a previous date :)
Hello Maddie, Is it possible to modify this code to display the day and month in the double digit format e.g. for 1 Jan 2024 = 01/01/2024?
Hi Royston,
I would say it is for sure possible. I don't know the exact answer since I came to this through a bit of trial and error. You could look at the following below and see if swapping it out at the right place gives you the format you are looking for
toString()
gives you Fri Jul 02 2021 14:03:54 GMT+0100 (British Summer Time)toDateString()
gives you Fri Jul 02 2021toLocaleString()
gives you 7/2/2021, 2:05:07 PMtoLocaleDateString()
gives you 7/2/2021toGMTString()
gives you Fri, 02 Jul 2021 13:06:02 GMTtoUTCString()
gives you Fri, 02 Jul 2021 13:06:28 GMTtoISOString()
gives you 2021-07-02T13:06:53.422Z