Forum Discussion
Javascript date format dd/mm/yy NOT dd/mm/yyyy - do I need to import a library?
//Call the StoryLine player
var player=GetPlayer();
//Get today's date from the computer
var today = new Date();
//Isolate the day, the month and the year from the date and convert them to text strings
var dd = today.getDate().toString();
var mm = today.getMonth()+1; //January is 0!
var mm = mm.toString();
var yy = today.getFullYear().toString().substr(-2);
//create a variable that combines the month and day strings
var year = mm+dd
//evaluate the new variable to see if it is past the target date and, if so, convert it to a number and add 1 to it. Note, July 24th is represented here by the number 724. Any dates past this will cause the year to be adjusted +1 from the current year. On January 1st (which will convert to 11) the future current date will be less than the target and the new future year will be used as is (meaning the year for January 1st 2018 will be 2018)
if(year>724) {
year = +yy + +1
} else {
year = +yy
}
//combine the date elements as mm/dd/yy
//start by creating a leading zero for single digit days and months
if(dd<10) {
dd='0'+dd
}
if(mm<10) {
mm='0'+mm
}
today = mm+'/'+dd+'/'+yy;
//Send variables to StoryLine
player.SetVar("Today's_Date",today);
player.SetVar("Year",year);