Update a variable with the current date and time

Jun 21, 2013

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.

39 Replies
Ashley Terwilliger-Pollard

Hi Martin,

I'm not sure if David is still subscribed here as I saw he also didn't answer Jacqui's similar question. I don't know much (or anything really!) about Javascript code, so I hope someone here in the community can pop in to assist. You could also try using the "contact me" button on a ELH users' profile to reach out to them directly for advice! Best of luck with your project! 

Michael Hinze

I think this simplified script here should do the trick;

var currentDate = new Date();
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = day + "/" + month + "/" +year
player.SetVar("DateValue", newName);
var currentTime = new Date();
var hours = currentTime.getHours();
var minutes = currentTime.getMinutes();
var CurTime = hours + ":" + minutes;
player.SetVar("TimeValue",CurTime);

Michael Hinze

I just realized my earlier script suggestion would not account for hours and minutes< 10. They would be shown like "9:9". See attached a quick update that fixes this issue to show the hours and minutes always with two digits, e.g. "09:09". Here is the published example.

Susan Jorstad

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!

Tiffany Dorris

Not sure if anyone still following this thread, I did reach out to some people individually. 

Here is my question:

I am using the following javascript to display the learner's current time in my course.

______________________

var player = GetPlayer();

function date_time() {
var date = new Date();
var am_pm = "AM";
var hour = date.getHours();
if(hour>=12){
am_pm = "PM";
}
if(hour>12){
hour = hour - 12;
}
if(hour<10){
hour = "0"+hour;
}

var minute = date.getMinutes();
if (minute<10){
minute = "0"+minute;
}


player.SetVar("12Hour", hour+":"+minute+" "+am_pm);

}

setInterval(date_time,500);

_________________________________

It is working beautifully. However, there are a few places where the learner completes a process and I want to take a timestamp of when they push the hotspot to start the process. So if they click the hotspot at 1:00 pm, it will show 1:00 pm on all the following slides. Is there a way to do this? I can't use the reference to the existing variable because the time changes. So I am assuming I need a new javascript for the timestamp. Any help would be much appreciated! (Note - I don't know how to write code, I just know how to copy and paste! Be gentle!) LOL

Thank you!