Fidning the difference between two dates in Storyline 2.

Apr 18, 2016

Hello all, I've read several helpful threads here that have gotten me to this point but now I seem to be extremely stuck. I'm building a series of triggers and variables to direct new employees to a specific portion on the course based on how long they have been with company. I'm using this javascript to set the variable for the current date:

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

This triggers when the timeline for the slide starts. Then the employee enters their specific start date which adjust the StartDate variable using simple text entry box. Everything up to this point works perfectly but here's where we go off the rails. Next, I need to calculate the difference between the current SystemDate and the employees StartDate and populate the difference into another variable, Difference. I'm trying to use this script but I'm getting nothing out of it:

 

var player = GetPlayer();

var date1 = player.GetVar("SystemDate");

var date2 = player.GetVar("StartDate");

var diff = player.GetVar("Difference");

 

diff = Math.round(Math.abs((date2.getTime() - date1.getTime())/(oneDay)));

 var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds;

 player.SetVar("Difference", diff);

If I can get this final variable to adjust I'm golden. So in utter defeat I turn to my more knowledgeable colleagues. Any help would be greatly appreciated

 Sincerely,

Allen McInnish

3 Replies
Alexandros Anoyatis

Hi Allen,

Your code syntactically checks out. Look for logical errors. Why are you GetVaring difference when that's what you need to set? Are your variables text? I can't see you parsing anything anywhere, which might be why your diff calculation might fail.

Firstly, I would rewrite this as :

var player = GetPlayer();

var date1 = player.GetVar("SystemDate");

var date2 = player.GetVar("StartDate");

var oneDay = 24*60*60*1000; // hours*minutes*seconds*milliseconds;

var diff = Math.round(Math.abs((date2.getTime() - date1.getTime())/(oneDay)));

player.SetVar("Difference", diff);


This might not even be enough - it depends on how you've set your StartDate and Difference variables in Storyline, as well as how you want these displayed.

Personally I would forget about the script above (for example it does not take into account leap dates and/or daylight savings etc) and I'd use moments.js which would have done all the hard work for me. Then I could parse the resulting difference or compare depending on my set conditions.

Having said that, sorting out your logical error above might be quicker and close enough to the right solution.

Hope this helps,
Alex

This discussion is closed. You can start a new discussion or contact Articulate Support.