Javascript date format dd/mm/yy NOT dd/mm/yyyy - do I need to import a library?

Jun 08, 2017

I am developing a software simulation which uses dates/ calendars extensively. (a booking system) Some javascript has already been used in this project, for example system date, year, month, day of the week etc. But I am now at the limit of our in-house knowledge. We specifically need some code for displaying system date in a dd/mm/yy format as opposed to the standard dd/mm/yyyy, as this doesn't match the original piece of software we are simulating. Do I need to pull in a library?? If so which one and how??

I have a few further issues as well - here is the scenario - we are forcing the student to book specific dates as to avoid having to even think about creating a perpetual calendar or limiting the shelf life of the course to a few years for example, but in order to do this  I need some way of forcing the year to jump to the following year if the date is past 24/07 for example in any given year ….. as a user cannot book a date in the past - so my question is can I work with a combination of Javascript variables and normal variables  - so something like, IF %SystemDate% is greater than or equal to 24/07/%Year% THEN  hide text box xyz that contains %Year% and Show text box abc that contains %Yearadd1%??? Well done for following thus far!!! I can't post the file as it is NDA, but I can try and strip it out if necessary for posting.

ANY help super appreciated :)

12 Replies
OWEN HOLT

To answer part 1 of your question, you current JavaScript probably contains some script similar to this to get the system date: var today = new Date();
and something like this to isolate the year:  var yyyy = today.getFullYear();
To convert the year to 2 digits, change the value returned for the year above into a string and then create a substring of that using the last 2 digits like this: var yyyy = today.getFullYear().toString().substr(-2);

For part 2, I need more info. How are they choosing the date? Do they type in the full date in an entry field or fields? Do they click a date on a calendar? Do they choose a date from a list of available dates? 

OWEN HOLT

Part 1 JS:

//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
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yy = today.getFullYear().toString().substr(-2);

//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 to StoryLine variable
player.SetVar("Today's_Date",today);

Donna Westwood

You sir, are a super star!!

The second part - we are predefining the date - based on when we take the screen recordings - so the user clicks the hotspot on a calendar (but they only have one option ) in a try me scenario - so there isn't any actual date in the system - just what appears visually - does this make sense?? in this particular case it is  24/07/17.... but if I test the course in August for example we wouldn't want the user to book in the past - the year would need to jump forward, I have a current variable based on system date that just shows 2017 called %year% this is what would need to jump to 2018 if the date the user was taking the course had passed 24/07. But it must be based on the system date and jump one year forward - so a user taking the course in August 2018 would see 2019 for example??

Thankyou SOOOOO Much!!!

OWEN HOLT

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

Math Notermans

Although Vikas idea to simplify getting the current Date is usefull, when anyone is trying this, do remember 2 things.

Anywhere you see $ - signs in Javascript code, jQuery is used. And as this isnot available by default in Storyline, you will have to add the jQuery library to your Storyline. As the same is true for MomentJS. As this also is a separate JS library you need to add it to get working.