Forum Discussion
Update a variable with the current date and time
Is it possible to insert the date and time into a variable to be displayed on screen.
What I want to achieve:
To time an activity that takes place over a number of slides.
Happy to use Javascript - any help is appreciated.
40 Replies
- PamLeeCommunity Member
Hi Ashley, thank you; I'm happy to be here! Michael, thanks for answering my post so quickly. I'm traveling now, but I'll test drive it as soon as I get in.
Pam
Well we're happy to have you!
- PhilEaglesCommunity Member
Hi, this is a great solution. I'm no expect in Javascript so I'm struggling to work this out. I would like to know what the code is to display the date 3 months ago. So the current date minus 3 months.
Any help much appreciated.
Thanks
Phil
Hi Phil,
This thread is a bit older so I"m not certain that folks are still subscribed, but you may want to use the "contact me" feature on their profiles to reach out direcly about how they may have accomplished this.
- PhilEaglesCommunity Member
Hi Ashley,
It's okay I think I have figured it out.
I changed the +1 value on the month to -1 instead and that brings me back to January which is correct :)
var month = currentDate.getMonth() - 1
Glad you were able to figure something out Phil and thanks for sharing it here as I know there are often users who are looking for Javascript solutions!
- PhilEaglesCommunity Member
I have actually found a flaw in my code. If I set my system time back to January 10th and run the script again, the date is displayed as 10/-1/15 rather than 10/10/14 I guess it's doing this because changing the month variable will not automatically change the year.
Any suggestions?
I am not equipped to help with Javascript issues, so I'll defer to the community on this set up.
- davidmckisickCommunity Member
I had issues getting this code to work, so I (not having any Javascripting experience) tinkered around with it until it did. Here is the code that does work which I have setup via two javascript triggers:
Trigger 1
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = month + "/" + day + "/" +year
player.SetVar("DateValue", newName);Trigger 2
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
if (hours <12)
var suffix = "AM";
if (hours >= 12)
var suffix = "PM";var CurTime = hours+":"+minutes+":"+seconds+" "+suffix;
player.SetVar("TimeValue",CurTime);To get this to work in Storyline, first setup two text variables, "DateValue", and "TimeValue". Then on your slide or master slide, setup a text box or object and then insert two references for DateValue and TimeValue. I simply set the triggers to execute Javascript when timeline starts.
You can modify the code easily by modifying the varCurTime, or var newName lines. I really am not sure what that nested statement in the time code was about in the original code but I could not get it to work.
Test file attached.
- ElainePretty-15Community Member
Thanks David! works perfectly
Glad this thread helped you out Elaine!
- SusanJorstad-e5Community Member
This is a great thread, just what I need but I cannot get the DateTime_Test.story file from David above to work. The date works, but not the time. See attachment. I didn't make any changes except downloading the .story file and upgrading it to SL 360 and publishing and uploading to Canvas LMS. Any suggestions? I really need a date and time stamped on the results slide, right down to the seconds like this example shows. I'm not super good at JavaScript, but I do understand the need to create variables and the basics of how this should work. For instance, I have a variable that calls up the user's name from Canvas, and that works . . . . I've spent a lot more time than I expected looking at other threads, and help would be appreciated!
Related Content
- 10 months ago
- 10 months ago
- 10 months ago