Forum Discussion

WilliamRyan-dba's avatar
WilliamRyan-dba
Community Member
7 days ago

Store today's date as a variable

Hi All, I'm curious how one would get today's date (or rather, the date that someone has taken a course) and store it as a text variable? It would be preferable to have it automatic, like when someo...
  • AlexMilyaev-f86's avatar
    7 days ago

    You need a code similar to this:

    // Get the current date

    var currentDate = new Date();

     

    // Array of month names

    var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

     

    // Get day, month, and year

    var day = currentDate.getDate();

    var monthIndex = currentDate.getMonth();

    var year = currentDate.getFullYear();

     

    // Format the date into a single string

    var formattedDate = months[monthIndex] + ' ' + day + ', ' + year;

     

    // Set the variable value in Storyline

    GetPlayer().SetVar('currentDate', formattedDate);

    You will insert this code into a trigger to run JS. 

    In Storyline, you need to create a text variable called currentDate and display it on the screen in the desired location.