Forum Discussion

WilliamRyan-dba's avatar
WilliamRyan-dba
Community Member
3 hours 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 someone clicks a button, rather than make them manually enter the date. Thanks in advance... 

  • Hi there, thanks for your reply. I'm looking for something close, basically date formatted "long date" (e.g., October 10, 2024)

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Cool, OK, this code can do both! I'm away from my desk right now, but I'll post it here in a couple of hours.

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Actually, just found the code in my cloud drive.

      Try this. 

      You'll need to create three TEXT variables to receive the data, named vTime, vDateShort and vDateLong.

      Trigger the code to run when your timeline starts.

       

       

      // Function to update the time and date variables
      function updateTimeAndDate() {
        // Get the current time and date
        var now = new Date();

        // Format the time in 24-hour format (HH:MM)
        var hours = now.getHours().toString().padStart(2, '0');
        var minutes = now.getMinutes().toString().padStart(2, '0');
        var vTime = hours + ":" + minutes;

        // Format the short date (MM/DD/YY)
        var month = (now.getMonth() + 1).toString().padStart(2, '0');
        var day = now.getDate().toString().padStart(2, '0');
        var year = now.getFullYear().toString().slice(-2);
        var vDateShort = month + "/" + day + "/" + year;

        // Format the long date (Month DD, YYYY)
        var monthNames = ["January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"];
        var vDateLong = monthNames[now.getMonth()] + " " + day + ", " + now.getFullYear();

        // Update the text variables in Storyline
        try {
          var player = GetPlayer();
          if (player && typeof player.SetVar === "function") {
            player.SetVar("vTime", vTime);
            player.SetVar("vDateShort", vDateShort);
            player.SetVar("vDateLong", vDateLong);
            console.log("Time set to: " + vTime);
            console.log("Short Date set to: " + vDateShort);
            console.log("Long Date set to: " + vDateLong);
          } else {
            console.error("GetPlayer() or SetVar function not available");
          }
        } catch (error) {
          console.error("Error setting variables:", error);
        }
      }

      // Call the updateTimeAndDate function once
      updateTimeAndDate();

  • 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.