Date Calculations in Lectora

Does anybody have experience with date calculations in Lectora? I am looking to perform a comparison between two dates: a date set in the past and the current date. If the difference between the two is greater than X, I want to update a variable.

For example, if I create a blank variable as Completion_Date, I want to update this variable with the current date the first time a user completes the course. This date would not update unless a 30 day time period has passed. If this time period has passed, the Completion_Date would update with that new date.

In context: A user completes a course on 5/1/2016 and their Completion_Date is marked as such. If they go in on 5/2/2016 - 5/31/2016, the Completion_Date would remain the same. If, however, they go in on 6/1/2016, I would like the Completion_Date to update to 6/1/2016.

I realize the simple answer to this is LMS management, but I am more just curious to see if there is a simple way to achieve this in Lectora. When I look at the conditions in Lectora, I see the comparative clauses (Relationships), but how can I interpret the text date (Thursday, May 26, 2016) into a string (05262016) on which to perform the calculation?

Discussion (7)

I attended an LUC session where we talked about a similar topic. We created a variable called CurrentDate and used the LMS variable to find the current date based on our computers. Then added another variable for the submission date. We then said that if current date is not equal to your submit date then prompt this message. If you applied the same principals you could also trigger a variable change or other type of action.

I did this demo (attached) a long time ago for someone on the Lectora forums. See if it is of any use, please don't hesitate to ask questions if you need help.

Thank you both. I have yet to do a deep-dive on Sergey's suggestion, but I will give it a shot. As to your suggestion, Jennifer, that's more or less how I'm performing this task today; however, I don't think this quite fits my need. Where would the elapsed time period calculation come into place?

If I completed a course a week ago, the LMS would have recorded the Completion Date at 5/24/2016. I want to show this date in the course (we'll call it, "Finish Date"). I can use a variable to check against that date and populate the current date (5/31/2016) but I don't want it to do so until 30 days have passed. Meaning, I don't want that "Finish Date" to change unless it is 6/23/2016.

I am working on something similar, but I am having an issue with the JavaScript updating the Lector variables.

Based on the Example Sergey provided, (not changing variable names to keep it simple) I am setting the user defined variable "MyMonkeyExpired" = "0" first to make sure the variable is initialized each time I load the page. Then I run the JavaScript Provided by Sergey

var d = new Date();

var ex = new Date("09/26/2016"); //set this by hand as MM/DD/YYYY

Math.floor((Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())-Date.UTC(ex.getFullYear(), ex.getMonth(), ex.getDate()))/(1000*60*60*24))> 45?

VarMyMonkeyExpired.set =('YES') : VarMyMonkeyExpired.set = ('NO');

Then Based on the value of "MyMonkeyExpired" I display a popup. I am finding that once I set the value of "MyMonkeyExpired" to "0", the JavaScript is not changing the variable to "Yes" or "No". I tested the code in a tool on and it appears to work great there using a slight alteration of the variable to reference an object on that site.

So my question, is "VarMyMonkeyExpired.set =('YES') : VarMyMonkeyExpired.set = ('NO');" the proper way of setting a user defined variable from JavaScript?


I got this working, but I had to change the code slightly. It appears the "=" was causing me grief. While I was tweaking I updated the variables and changed the "if -else" so other developers could follow along behind me a little easier.

var d = new Date();

var ex = new Date("7/15/2016"); // undefined----------- set this by hand as MM/DD/YYYY this should be the approximate go live date

if(Math.floor((Date.UTC(d.getFullYear(), d.getMonth(), d.getDate())-Date.UTC(ex.getFullYear(), ex.getMonth(), ex.getDate()))/(1000*60*60*24)) > 45)








What I would like to do is grab the lectora "PublishDate" and use that for my comparison so I do not need a developer to enter a date. I am having a problem getting the date in a format needed to calculate it.

undefinedpre style="padding-left: 30px;">Var_retakeDate.set(Math.round(new Date().getTime() / 1000)+864000);undefinedThat stores that moment in time as seconds (since 1/1/1970) plus 10 days worth of seconds.undefinedpre style="padding-left: 30px;">Var_todaysDate.set(Math.round(new Date().getTime() / 1000));undefined0. But for our situation, the target date would always be different, and needs to be remembered so it is stored as a Lectora variable.undefined

Since I'm home, I can't look at your sample - However, I had what I think is a similar question several months ago where I wanted to set a drop down to display a range of dates based on the current date:

Dynamic date as initial value...

If I understand correctly, you're getting the date that the user last took the course then restricting retaking it for 10 days from the initial completion date. Using a similar method, you could change the "-30" to "-10" and do the compare, thus overcoming the need for the math function. The tricky part might be if the user took the course on 1/30 and is trying again on 2/10, which is technically 11 days later.

Discussions have been disabled for this post