Forum Discussion
Adding current date not working
Hi. I'm trying to add the current date using a variable trigger but cannot seem to get it to populate. I have the correct javascript and it doesn't work when published. I'm at a complete loss at to what I might be missing. I have followed the steps as shown below. Is this the system being glitchy or am I missing something?
1. Create a Variable
- Go to "Manage Project Variables".
- Create a new Text variable, e.g., name it CurrentDate.
2. Insert a Text Box
- Insert a text box on your slide.
- Set its content to:
Today is %CurrentDate%
(or however you want the date displayed)
3. Add a Trigger to Execute JavaScript
- Add a trigger on the slide:
- Action: Execute JavaScript
- When: Timeline starts (or however you want to trigger it)
- Code:
- javascriptvar player = GetPlayer(); var today = new Date(); var dd = String(today.getDate()).padStart(2, '0'); var mm = String(today.getMonth() + 1).padStart(2, '0'); // January is 0 var yyyy = today.getFullYear(); var formattedDate = mm + '/' + dd + '/' + yyyy; // You can change to dd/mm/yyyy if needed player.SetVar("CurrentDate", formattedDate);
- CopyEdit
4. Preview or Publish
- In Preview mode, JavaScript won’t always work.
- You’ll need to publish the course (HTML5 output) and view it in a browser for the JavaScript to take effect.
EDIT: Is see in the console a reference to SystemDate, but the JavaScript is attempting to set CurrentDate. Can you confirm you Storyline variable is named CurrentDate and referenced as %CurrentDate% in the tex box
-------------
Do you mind sharing you .story file. You can just attach to the post. One thing I would check is just make sure your variable CurrentDate is spelt correctly and does not contain whitespace before or after it wherever it is referenced.
Just copy one correct instance and then paste wherever it is referenced to be sure.
Happy to check your file if no luck still.
7 Replies
- SamHillSuper Hero
Hi soucie what problem are you seeing?
There doesn't appear to be any issues with the script and functionality you have described. This works fine for me in Preview and when published.When you publish and attempt to run, and it doesn't work, hit F12 on your browser, and check the tab labelled "console". Check for any errors in there. They will be marked with red text. Paste them into this discussion. It may be a browser/environment specific issues. Having said that though, there isn't anything that is pushing the boundaries in the JavaScript. All pretty standard stuff.
FYI: JavaScript does now execute in Preview mode, so you should see the date.- soucieCommunity Member
Thank you for confirming I set it up correctly; here are the errors I get. Any idea what I would need to do to fix this? I am so grateful for you chiming in on this!
- SamHillSuper Hero
EDIT: Is see in the console a reference to SystemDate, but the JavaScript is attempting to set CurrentDate. Can you confirm you Storyline variable is named CurrentDate and referenced as %CurrentDate% in the tex box
-------------
Do you mind sharing you .story file. You can just attach to the post. One thing I would check is just make sure your variable CurrentDate is spelt correctly and does not contain whitespace before or after it wherever it is referenced.
Just copy one correct instance and then paste wherever it is referenced to be sure.
Happy to check your file if no luck still.
- soucieCommunity Member
The issue I get is nothing displays in the field where the date should display. I'm at a total loss on this one!
- PhilMayorSuper Hero
Javascript now works in preview and there is also access to the console to debug.
sharing the file would make life easy to debug