JavaScript Current Date Coding - Using Full Month, Day and Year (i.e. January 1, 2015)

Feb 20, 2015

I would like to add the date variable to a completion certificate. I have the JavaScript coding to capture the current system date, however, I want to ensure that the date locks and doesn't change each time the SCORM is accessed. Also, I want to display the date as full month description, day and year (i.e. January 1, 2015). Does anyone have the JavaScript coding for full month, day and year, with the lock down feature?

 

Thanks

Sandy

33 Replies
Jackson Hamner

This is the code I use. Just use SetVar on the date variable and it should display in the formatting you want.

 

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
if(dd<10) { dd='0'+dd }
var date= m_names[mm]+' '+dd+', '+yyyy;
RECO Education

Thanks Jackson. Would I use this SetVar code in addition to the current date code? Or just this SetVar code? The JS code I have for current date is:

 

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

Jackson Hamner

Replace your current code -

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

with -

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September",
"October", "November", "December");
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();
var yyyy = today.getFullYear();
if(dd<10) { dd='0'+dd }
var date= m_names[mm]+' '+dd+', '+yyyy;
var player = GetPlayer();
player.SetVar("SystemDate",date);

 

And it should work great. Make sure to replace the "SystemDate" with the name of whatever variable you're using to story the date value in storyline.

RECO Education

Hi Jackson, not sure if you have the answer to this but is there a way to lock in the date. When I accessed my test SCORM file today, the date changed from yesterday's date to today's date. Is there a way to capture the date of completion to lock the date and not cause it to change when learners go back in.

Alicia Blitz

I am looking for some JS to convert a date to the Day of the Year (Julian). I want to use the DOY and year to time stamp when a user first enters a course. When they reenter I want to compare the dates.

My end goal is to make a user restart a course if they haven't finished within a certain number of days of starting.

I though the Julian Day and current year would be easier to control and compare.

Michael Hinze

To get the Julian day, try this:

"

Date.prototype.getDOY = function() {
var onejan = new Date(this.getFullYear(),0,1);
return Math.ceil((this - onejan) / 86400000);
}

var today = new Date();
var daynum = today.getDOY();

var player = GetPlayer();
player.SetVar("TodaysJulianNumber",daynum);

"

In this example, I used a Storyline variable 'TodaysJulianNumber' to store the Julian day number. 

Jackson Hamner

Here ya go buddy:

function formatAMPM(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var ampm = hours >= 12 ? 'PM' : 'AM';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ' ' + ampm;
return strTime;
}

var weekday = new Array(7);
weekday[0]= "Sun";
weekday[1] = "Mon";
weekday[2] = "Tue";
weekday[3] = "Wed";
weekday[4] = "Thu";
weekday[5] = "Fri";
weekday[6] = "Sat";

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1;
var wd = weekday[today.getDay()];
var yyyy = today.getFullYear();
var time = formatAMPM(today);
var date= wd+' '+mm+'/'+dd+'/'+yyyy+' ' +time;

var player = GetPlayer();
player.SetVar("date",date);

 

 

Jackson Hamner

Hi Alicia,

Basically yes, the final variable 'date' in the script is being pushed to the storyline variable.

If its not working I would double check the name of your storyline variable. Mine was also named date, but perhaps you named yours 'systemDate' or something like that?

I would also double check that your browser is enabled to view javascript just in case thats the problem.

Diana Myers

@All - I thought this was so cool (THANKS JACKSON!!!), but when I tried it, it didn't work the first time.  I went back in and discovered that variables are case sensitive. 

I initially added a variable called "Date" and then executed Jackson's script exactly and it didn't work.  Once I updated the variable to "date" it worked like a dream! 

Thanks again! 

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