Forum Discussion
Store today's date as a variable
- 3 months 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.
Great, thanks!
- Jonathan_Hill3 months agoSuper 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();