Forum Discussion

SteveMarlow-0fc's avatar
SteveMarlow-0fc
Community Member
3 years ago

Javascript variable / Current Date

Hello, Can someone give me the javascript to display the current date by using a variable? It is similar to the below javascript but I need it to display the date numerically like "06/20/2023" 

var options = {weekday: 'long', year: 'numeric', month: 'long', day: 'numeric', hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }; var date = new Date().toLocaleTimeString('en-us', options); var player = GetPlayer(); player.SetVar("SystemDate",date);

16 Replies

  • I found this solution. Thanks. 

    %todaysDate%

     

    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);

  • Hi Steve,

    You can control the format by adjusting which options you set and what values you provide for them. For example, this set of options will give you 2 digits for the month, 2 digits for the day, and 4 digits for the year:

    var options = {month:"2-digit", day:"2-digit", year:"numeric"};
    var date = new Date().toLocaleDateString("en-us", options);

     

    You can also skip the options entirely to get numeric values for all three (e.g., "6/20/2023"):

    var date = new Date().toLocaleDateString("en-us");