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
Sinchu Raj

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);

DateValue is my variable in the storyline, it should be a text value

Sinchu Raj

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()
if (minutes
minutes = "0" + minutes
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
var CurTime =  hours + ":" + minutes + " " + suffix ;
player.SetVar("TimeValue",CurTime);

Michael Hinze

Pam Lee said:

I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?


You add a Excute Javascript trigger to an object, e.g. a button and then place the JavaScript inside the JavaScript window. See below a screenshot of a button that when clicked executes the date-related JavaScript listed in this thread. You can also place your code into an external file and call that function from the trigger.

Michael Hinze

Michael Hinze said:

Pam Lee said:

I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?


You add a Excute Javascript trigger to an object, e.g. a button and then place the JavaScript inside the JavaScript window. See below a screenshot of a button that when clicked executes the date-related JavaScript listed in this thread. You can also place your code into an external file and call that function from the trigger.


Sorry, forgot to include this link to some samples and demos that may be helpful: http://www.articulate.com/support/storyline/javascript-best-practices-and-examples

david mckisick

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.

sohail arif

In JavaScript you can create date & time objects using the Date constructor. Then object can use its own methods for different computations afterward.

There are number of ways in which you can create the date object via Date constructor.

Example

var today_date_time = new Date();                         // Get today's date and time
var old_date = new Date(2010, 4, 1);                       // Date Literal
var old_date_time = new Date(2010, 0, 1, 18, 10, 30);   // Date literal + time
var elapsed = today_date_time - old_date ;            // Get the interval after subtraction in milliseconds
      
      // Methodsold_date.getFullYear()  // get year
old_date.getMonth()     // get month number (starting from zero)
old_date.getDate()        // get day
old_date.getDay()          // get day of week (0-6) Starting from Sunday: 0
old_date.getHours()      // get local time
old_date.getUTCHours()   // get time in UTC

 

 

See working example at:

http://www.thesstech.com/javascript/date-time