Forum Discussion
Convert systemDate to static txt
I created a variable, systemDate, using JavaScript to display the completion date on a certicate. However, the date changes when the certificate is revisited.
Does anyone know how I can convert the date to static text so if the learner returns to the certificate at a later date, the certificate will display the original completion date?
I attached a demo copy of the template and an image of the certificate.
- PhilMayorSuper Hero
how do you pull the date is it going into a storyline variable? You could set it so it only works if the initial variable is blank, but would be reliant on resume working
- TinaDenmark-558Community Member
I thought the copies were attached. I just attached them now.
The variable is blank and is generated via a JavaScript:
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=month + "/" + day + "/" + year
var player = GetPlayer();
player.SetVar("systemDate",dateString); - NedimCommunity Member
Try this code:
var dateString;
// Check if dateString has been previously defined
if (typeof dateString === 'undefined') {
// If not, generate the date
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
dateString = month + "/" + day + "/" + year;
var player = GetPlayer();
player.SetVar("systemDate",dateString);
}
In case the provided code doesn't function as intended, an alternative approach is to store the 'dateString' in a variable or a persistent storage medium, such as a localStorage. By doing so, you can easily retrieve the stored date whenever required. - TinaDenmark-558Community Member
I'm not sure I have the script provided above set up correctly.
I copied everything from // the } and placed it in the JavaScript for the trigger. See the attached for what my Variables look like. I think I may be missing something or may not have the variables set properly to transfer the date correctly.
- NedimCommunity Member
Tina,
You should have included var dateString; in the first line of your code as well. In this code, you're setting the value of the "systemDate" Storyline variable to the value of the "dateString" variable. Therefore, you should reference the "systemDate" variable on your slide, not the "dateString" variable as indicated in your document.- TinaDenmark-558Community Member
Hi,
Thanks for the quick reply.
I now have the JavaScript in the trigger like this:
[cid:image001.png@01DA802A.DBB4C550]
I have also put the systemDate back into the certificate. I see today’s date when I run the slide.
I will verify the date is static tomorrow morning.
Thanks, Tina
- NedimCommunity Member
Your image didn't come through, but you can adjust the system date on your computer to verify the date on the certificate. I'm not sure how you preview your course, but to ensure the code is functioning correctly, I recommend running it inside the LMS or on the web. It won't work in Storyline preview or Review 360.
- PhilMayorSuper Hero
IT should now work inside Storyline as Javascript runs in preview
- NedimCommunity Member
That's correct, Phil. However, with this code, the value of the 'dateString' variable will not persist, so during preview, it will keep displaying today's date. When you come back to it tomorrow, it will display 3/28/24. I was trying to achieve something else with less code. The localStorage version should look something like this:
var dateString = localStorage.getItem('certificateDate');
if (!dateString) {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
dateString = month + "/" + day + "/" + year;
localStorage.setItem('certificateDate', dateString);
}
var player = GetPlayer();
player.SetVar("systemDate", dateString);
This code will run properly in preview as well.
Tina,
I have attached the .story file for reference. Please disregard the first JavaScript trigger, as it always shows the current date.- PhilMayorSuper Hero
Always helps if I read the whole thing
- NedimCommunity Member
Lol... No worries, Phil. Your posts and comments are incredibly valuable here. Asking questions, raising concerns, and correcting mistakes are crucial; it's how we obtain the answers we need.
- TinaDenmark-558Community Member
OK, do I need to per define certificateDate a Variable first, then add the new script to the JavaScritp link?
var dateString = localStorage.getItem('certificateDate');
if (!dateString) {
var currentTime = new Date();
var month = currentTime.getMonth() + 1;
var day = currentTime.getDate();
var year = currentTime.getFullYear();
dateString = month + "/" + day + "/" + year;
localStorage.setItem('certificateDate', dateString);
}var player = GetPlayer();
player.SetVar("systemDate", dateString);Thanks, Tina
- NedimCommunity Member
...'certificateDate' could be anything, no need to declare. Just make sure it's consistent through out the code. It's used twice in this code.
- TinaDenmark-558Community Member
The story file was not included.
Thanks,
Tina D Denmark CHAM CHAA, MCCT(r)
Sr. Learning Development Specialist
Learning, Leadership, & Organizational Development, Learning Technology Team[Adobe Captivate Specialist (2019 release)][cid:image002.png@01DA803F.48C33AA0]
[cid:image003.png@01DA803F.48C33AA0]
820 West Diamond Ave
Gaithersburg, MD 20878
Email: tdenmark@adventisthealthcare.com
Office: 301.315.3553 | Fax: 301.315.3053
Website: www.AdventistHealthCare.com
[facebook][twitter] [youtube] [LinkedIn] [rss]Need help with Workday Learning?
Please note: All technical/access issues, questions about assignments, or requests for Learning support must be initiated in Ivanti, our IT Ticketing System platform. Either directly into Ivanti or by calling the IT Help Desk at 240-826-6440.
The Learning Technology Team hours are:
Mon. - Th.: 8 am - 4:30 pm
Fridays: 8 am - 3 pm
The Learning Technology Team responds within 5 business days, sooner if possible.
- NedimCommunity Member
It is ok, Tina. The 'certificateDate' is something like a key or identifier used to retrieve data from the localStorage object. Keep everything as it is on your slide. Don't reference 'certificateDate', keep the systemDate. I am attaching .story file again. And a screenshot.
- TinaDenmark-558Community Member
Hi Nedim,
I reopened the course this morning to see the certificate is displaying today's date, not yesterday's date of 3/27/2024. I'm not sure what I am missing? See the attached screen shots and script used.