Forum Discussion
Show a date X number of days before today's date on Storyline - is this possible?
I have figured out how to pull today's date using Javascript. However, I want to generate a date that is X amount of days before today's date. For example, today shows as 30/08/2023. I want to show 01/08/2023 on the same slide.
The javascript I have used to show todays date is:
let currentTime = new Date();
let month = currentTime.getMonth() + 1;
let day = currentTime.getDate(); let year = currentTime.getFullYear();
// Putting it together let dateString = day + "/" + month+ "/" + year;
//Pushing data to Storyline let player = GetPlayer(); player.SetVar("todaysDate", dateString);
7 Replies
- RandyWalker2Community Member
/* Create new variable to hold date */
var d = new Date();
/* For example: write today's date in specified locale's format 29/08/2023 */
document.write('Today is: ' + d.toLocaleDateString("en-GB"));
/* Set our variable to the date of 5 days ago. We can do the math right here! You could use a variable here if it's something you want to change */
d.setDate(d.getDate() - 5);
/* Example: write out 5 days ago in specified locale's format 08/24/2023 */
document.write('<br>5 days ago was: ' + d.toLocaleDateString("en-US")); - TinaGlynn-59324Community Member
I'm also looking for Javascript to show 15 days prior to today's date in the format of MM/DD/YYYY, but everything I've tried so far in the wrong format or shows a negative number for the date. The previous reply shows it in a document, but I need to use it as a variable in Storyline. I'm new to Javascript, so I'm not sure what changes need to be made.
- JordanBestCommunity Member
Hi Tina, you can try this code:
var currentDate = new Date();
var fifteenDaysAgo = new Date(currentDate);
fifteenDaysAgo.setDate(currentDate.getDate() - 15);
// Format the date
var formattedDate = (fifteenDaysAgo.getMonth() + 1) + '/' + fifteenDaysAgo.getDate() + '/' + fifteenDaysAgo.getFullYear();
var player = GetPlayer();
player.SetVar("YourVariableName", formattedDate);
// you'll need a text variable titled "YourVariableName" in SL- TinaGlynn-59324Community Member
Jordan,
That worked perfectly. Thank you!
- DanielleRyan-0cCommunity Member
Leaving a message here on the off chance Jordan or anyone else sees it. I found this and used it today, however I tried to switch the script around a little so the date format is the UK format DD/MM/YYYY...I've somehow ended up with a date longer than 15 days ago (09/06/2024 and it's 23/07/2024 today) any idea what has happened there? I've never used JavaScript before.
Another question, can I use more than one 'lot' of JavaScript on one page? I would ideally like to have 4 dates on the page, all in the past.
Thank you!
- NedimCommunity Member
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);- DanielleRyan-0cCommunity Member
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.