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
Jürgen Schoenemeyer

here is an example to add/subtract days/months/years from the current date

function calculateDateDay( days ){
    const myDate = new Date();
    myDate.setDate(myDate.getDate() + days)
    
    return result = myDate.toLocaleDateString('en-US', {
        year:  'numeric', 
        month: '2-digit', 
        day:   '2-digit'     
    });
}

function calculateDateMonth( months ){
    const myDate = new Date();
    myDate.setMonth(myDate.getMonth() + months)
    
    return result = myDate.toLocaleDateString('en-US', {
        year:  'numeric', 
        month: '2-digit', 
        day:   '2-digit'     
    });
}

function calculateDateYear( years ){
    const myDate = new Date();
    myDate.setFullYear(myDate.getFullYear() + years)
    
    return result = myDate.toLocaleDateString('en-US', {
        year:  'numeric', 
        month: '2-digit', 
        day:   '2-digit'     
    });
}

var player = GetPlayer();

player.SetVar( "date0", calculateDateDay(0) );   // today
player.SetVar( "date1", calculateDateDay(-10) );  // -10 days
player.SetVar( "date2", calculateDateMonth(-2) ); // -2 months
player.SetVar( "date3", calculateDateDay(-90) );  // -90 days
player.SetVar( "date4", calculateDateYear(-1) );  // -1 year

result:

https://360.articulate.com/review/content/773e8a5c-fbb4-41d3-b2cd-0efb109cfa9f/review

 

Maddie Schroeder

Solution: 

For displaying today's date:

 //Today's Date

var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=month + "/" + day + "/" + year
var player = GetPlayer();
player.SetVar("todaysdate",dateString);


Maddie Schroeder

Solution

For subtracting days: 

var player = GetPlayer();

var myNewDate=new Date();
myNewDate.setDate(myNewDate.getDate() - 7);

var month = myNewDate.getMonth() + 1
var day = myNewDate.getDate()
var year = myNewDate.getFullYear()
var dateString=month + "/" + day + "/" + year

player.SetVar("Date",dateString);
Maddie Schroeder

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 

  1. toString() gives you Fri Jul 02 2021 14:03:54 GMT+0100 (British Summer Time)
  2. toDateString() gives you Fri Jul 02 2021
  3. toLocaleString() gives you 7/2/2021, 2:05:07 PM
  4. toLocaleDateString() gives you 7/2/2021
  5. toGMTString() gives you Fri, 02 Jul 2021 13:06:02 GMT
  6. toUTCString() gives you Fri, 02 Jul 2021 13:06:28 GMT
  7. toISOString() gives you 2021-07-02T13:06:53.422Z