Forum Discussion
Show a date X number of days before today's date on Storyline - is this possible?
Hi Danielle,
I think it is is because, for UK format, you need to ensure that the day and month are always two digits. I don't know how your modified script looks like but here is the code to properly show UK format with additional four more dates showing XX number of days in the past. I also attached .story file for a reference.
// Function to format date in DD/MM/YYYY for UK format
function formatDateUK(date) {
const day = String(date.getDate()).padStart(2, '0');
const month = String(date.getMonth() + 1).padStart(2, '0');
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
// Function to set Storyline variable with formatted date
function setStorylineVariable(variableName, daysAgo) {
const currentDate = new Date();
const pastDate = new Date(currentDate.setDate(currentDate.getDate() - daysAgo));
const formattedDate = formatDateUK(pastDate);
const player = GetPlayer();
player.SetVar(variableName, formattedDate);
}
// Set Storyline variables for different days ago
setStorylineVariable("fifteen", 15);
setStorylineVariable("twenty", 20);
setStorylineVariable("thirtyfive", 35);
setStorylineVariable("fifty", 50);
Thank you Nedim, that's genius and has worked a treat! I would never have been able to do that myself as my level of knowledge around JavaScript is simply copy and paste from these forums and see if it works! And thank you to the original poster for this thread, it's been very helpful and has made my project way more impressive.
Related Content
- 7 months ago
- 2 years ago
- 8 months ago
- 8 months ago
- 9 months ago