REQ: Javascript Date Variable Help.
Jan 18, 2013
Hi All,
I've managed to get it working correctly using the 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);
From this post: http://community.articulate.com/forums/p/12498/93743.aspx
And I've edited it so it displays in UK Format 18/1/13 rather than 1/18/13.
However, I was wondering if it's possible to have it showing as Friday, January 18th, 2013 as my course is themed as a Newspaper and I feel this would give it a nice touch.
Thanks in advance,
SCB.
20 Replies
No worries, sorted it now.
Please see code below if it's of any use to anyone:
var d = new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName=new Array("January","February","March","April","May","June","July","August","Septemeber","October","November","December");
var dateVal=d.getDate();
var delimeter=", ";
var delimeter1=" ";
var txtPostdate=""
if ((dateVal>=4 && dateVal=24 && dateVal
else if (dateVal==3 || dateVal==23){txtPostdate="rd";}
else if (dateVal==2 || dateVal==22){txtPostdate="nd";}
else if(dateVal==1 || dateVal==21 || dateVal==31){txtPostdate="st";}
var dateString=weekday[d.getDay()]+delimeter+monthName[(d.getMonth())]+delimeter1+dateVal+txtPostdate+delimeter+d.getFullYear();
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
SCB.
where do you add this code in SL?
Hi Randall,
Well you can put this in any trigger. (eg start timeline of your first slide)
Just be aware that the variable 'SystemDate' must exist in your course.
This variable SystemDate, you can then display it wherever you want the systemdate to appear.
@Shane, there's a small spelling mistake in 'Septemeber'...
Cheers
Geert
Hi Geert,
Thanks for this, I hadn't noticed!
Amended it in my source.
Cheers
SCB
Hmm, I was playing around with this today. The simple code in the first post works for me.
However, the code in the verified answer does not. I see nothing on the page when I view it.
Can't see what I'm doing wrong.
BTW, I'm viewing the content online from a LMS. On the same page, I'm pulling the learner name from the LMS with another piece of JavaScript.
Again, when I use the code in the first post, both values (the learner name and the date) show up perfectly. However, when I use the date code in the verified answer, nothing shows, not even the learner name which worked before.
Hey, Gerry -
The script is broken at this line:
if ((dateVal>=4 && dateVal=24 && dateVal
Easiest fix is to delete this part. This'll do away with the 'th', 'rd', 'nd', and 'st'.
if ((dateVal>=4 && dateVal=24 && dateVal
else if (dateVal==3 || dateVal==23){txtPostdate="rd";}
else if (dateVal==2 || dateVal==22){txtPostdate="nd";}
else if(dateVal==1 || dateVal==21 || dateVal==31){txtPostdate="st";}
There is a piece missing in the parens and the {} for the first conditional is MIA. The forum could have borked the copy and paste. It's happened to me before.
That's why it's not working.
Steve
Gerry -
Replace that bit I recommended deleting with this:
var lDigit=dateVal.charAt( dateVal.length-1);
if (lDigit=="1"){
txtPostdate="st";
}else if(lDigit=="2"){
txtPostdate="nd";
}else if(lDigit=="3"){
txtPostdate="rd";
}else{
txtPostdate="th";
}
This would make Shane's code into something that looked like:
var d = new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName=new Array("January","February","March","April","May","June","July","August","Septemeber","October","November","December");
var dateVal=d.getDate();
var delimeter=", ";
var delimeter1=" ";
var txtPostdate=""
var lDigit=dateVal.charAt( dateVal.length-1);
if (lDigit=="1"){
txtPostdate="st";
}else if(lDigit=="2"){
txtPostdate="nd";
}else if(lDigit=="3"){
txtPostdate="rd";
}else{
txtPostdate="th";
}
var dateString=weekday[d.getDay()]+delimeter+monthName[(d.getMonth())]+delimeter1+dateVal+txtPostdate+delimeter+d.getFullYear();
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
Different ways to get to the same ends
Another small correction. Need to convert the dateVal to a string to make my modification work.
That makes this line:
var dateVal=d.getDate();
into this:
var dateVal=d.getDate().toString();
Makes the enchilada into:
var d = new Date();
var weekday=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var monthName=new Array("January","February","March","April","May","June","July","August","Septemeber","October","November","December");
var dateVal=d.getDate().toString();
var delimeter=", ";
var delimeter1=" ";
var txtPostdate=""
var lDigit=dateVal.charAt( dateVal.length-1);
if (lDigit=="1"){
txtPostdate="st";
}else if(lDigit=="2"){
txtPostdate="nd";
}else if(lDigit=="3"){
txtPostdate="rd";
}else{
txtPostdate="th";
}
var dateString=weekday[d.getDay()]+delimeter+monthName[(d.getMonth())]+delimeter1+dateVal+txtPostdate+delimeter+d.getFullYear();
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
THANKS, STEVE!!!!!!!
I'll give it a whirl right now.
WOO-HOO! Worked great.
As usual, Steve, you just ROCK!!!
Thanks again!
Gerry, tx for asking. I couldn't get it to work when I'd tried it a couple months ago, but figured it was me. Steve, Gerry's right. You DO rock.
Hey, Steve! I've got one more for you.
Using the JavaScript code that you provided--how would you modify it to show a date 365 days in the future?
After
"var d = new Date();"
add this code:
d.setDate(d.getDate() + 365); // add 365 days
Note: you can use negative numbers to go back in time (e.g. -7 instead of +365 goes back in time 1 week).
Best wishes,
Paul K.
Yep. Paul's got it. Pretty flexible using the date object.
Thanks again, Guys!
Worked great. Much appreciated.
I needed this because I was finally getting around to playing with certificates of completion for Storyline courses in a LMS--especially with pulling in the name from the LMS and adding automatically dates.
Rather than us your great method here, Steve, of a separate HTML file for the certificate, I wanted to play around with having the certificate be a slide.
My client may have a need for this very shortly and some of their non-techie developers may prefer a slide solution as opposed to an HTML file.
Worked great. A couple of minor print issues (e.g., for FireFox on a PC, don't use the print button--right-click on the slide itself and chose Print) but nothing major so far--knock on wood
--in testing on IE9, FF18, and Chrome. For my iPad, I don't have a printer supporting AirPrint so I had to capture a screenprint with the iPad and then print that with my Epson printer's iPrint.
My client is doing handsprings so far as the certification certificate using the LMS's report for this is drab and not very customizable without using domains in the LMS for each course provider--and even then they only get one completion certificate template per domain.
I'll probably add a lightbox slide for printing help for various browsers and devices and have that appear when the cert slide is first accessed. Then add a button on the cert to bring back printing help if needed.
Probably also add some references for re-launch (that appear only when the course has been completed--maybe on the title slide and the final quiz intro) that lets the learner know they previously completed this and to jump right to their cert if desired.
Also like the fact that with the usual Storyline magic with triggers and variables and resume, the data gets saved in the LMS (and returned at re-launch--at least with this LMS) so if a learner goes back to print out the cert again, the original dates are retained.
So, anyways, THANKS again.
Hi Gerry,
Can you please explain how you got the original dates retained at resume? I've set the slide to “Resumesaved state” but it doesnt work, Did you apply a trigger and/or variable to resume the original dates at resume?
Any help would be greatly appreciated. Thanks!
Hi Alex,
I'm pretty sure Gerry wrapped his date call inside a conditional statement that checks to see if the currentDate and futureDate variables are null or have been previously set. If the variables are null then the date code executes, otherwise the JavaScript call is ignored and he reuses the "saved" variables to re-populate the form.
Or he used a completely different approach.
Thanks Stephen, will give it a try.
Hi Gerry,
I'm trying to achieve the same. That is, remember the date and time on a certificate - even if the course is closed and reopened.
Could you help me out with some Javascript?
Thank you!
This discussion is closed. You can start a new discussion or contact Articulate Support.