var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear(); var player = GetPlayer(); var newName = day + "/" + month + "/" +year player.SetVar("DateValue", newName);
DateValue is my variable in the storyline, it should be a text value
var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear(); var player = GetPlayer(); var newName = day + "/" + month + "/" +year player.SetVar("DateValue", newName);
var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() if (minutes minutes = "0" + minutes var suffix = "AM"; if (hours >= 12) { suffix = "PM"; hours = hours - 12; } if (hours == 0) { hours = 12; } var CurTime = hours + ":" + minutes + " " + suffix ; player.SetVar("TimeValue",CurTime);
I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?
I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?
You add a Excute Javascript trigger to an object, e.g. a button and then place the JavaScript inside the JavaScript window. See below a screenshot of a button that when clicked executes the date-related JavaScript listed in this thread. You can also place your code into an external file and call that function from the trigger.
I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?
You add a Excute Javascript trigger to an object, e.g. a button and then place the JavaScript inside the JavaScript window. See below a screenshot of a button that when clicked executes the date-related JavaScript listed in this thread. You can also place your code into an external file and call that function from the trigger.
I just wanted to welcome you to the E-Learning Heroes community! It looks like Michael has you covered here but if you need anything else just let us know!
Hi Ashley, thank you; I'm happy to be here! Michael, thanks for answering my post so quickly. I'm traveling now, but I'll test drive it as soon as I get in.
Hi, this is a great solution. I'm no expect in Javascript so I'm struggling to work this out. I would like to know what the code is to display the date 3 months ago. So the current date minus 3 months.
This thread is a bit older so I"m not certain that folks are still subscribed, but you may want to use the "contact me" feature on their profiles to reach out direcly about how they may have accomplished this.
Glad you were able to figure something out Phil and thanks for sharing it here as I know there are often users who are looking for Javascript solutions!
I have actually found a flaw in my code. If I set my system time back to January 10th and run the script again, the date is displayed as 10/-1/15 rather than 10/10/14 I guess it's doing this because changing the month variable will not automatically change the year.
I had issues getting this code to work, so I (not having any Javascripting experience) tinkered around with it until it did. Here is the code that does work which I have setup via two javascript triggers:
Trigger 1
var currentDate = new Date() var day = currentDate.getDate() var month = currentDate.getMonth() + 1 var year = currentDate.getFullYear(); var player = GetPlayer(); var newName = month + "/" + day + "/" +year player.SetVar("DateValue", newName);
Trigger 2
var currentTime = new Date() var hours = currentTime.getHours() var minutes = currentTime.getMinutes() var seconds = currentTime.getSeconds() if (hours <12) var suffix = "AM"; if (hours >= 12) var suffix = "PM";
var CurTime = hours+":"+minutes+":"+seconds+" "+suffix; player.SetVar("TimeValue",CurTime);
To get this to work in Storyline, first setup two text variables, "DateValue", and "TimeValue". Then on your slide or master slide, setup a text box or object and then insert two references for DateValue and TimeValue. I simply set the triggers to execute Javascript when timeline starts.
You can modify the code easily by modifying the varCurTime, or var newName lines. I really am not sure what that nested statement in the time code was about in the original code but I could not get it to work.
In JavaScript you can create date & time objects using the Date constructor. Then object can use its own methods for different computations afterward.
There are number of ways in which you can create the date object via Date constructor.
Example
var today_date_time = new Date(); // Get today's date and time var old_date = new Date(2010, 4, 1); // Date Literal var old_date_time = new Date(2010, 0, 1, 18, 10, 30); // Date literal + time var elapsed = today_date_time - old_date ; // Get the interval after subtraction in milliseconds
// Methodsold_date.getFullYear() // get year old_date.getMonth() // get month number (starting from zero) old_date.getDate() // get day old_date.getDay() // get day of week (0-6) Starting from Sunday: 0 old_date.getHours() // get local time old_date.getUTCHours() // get time in UTC
39 Replies
use javascript and set the value to a variabl in storyline
Do you know the code?
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = day + "/" + month + "/" +year
player.SetVar("DateValue", newName);
DateValue is my variable in the storyline, it should be a text value
Thank you - you are a star.
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = day + "/" + month + "/" +year
player.SetVar("DateValue", newName);
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes
minutes = "0" + minutes
var suffix = "AM";
if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
if (hours == 0) {
hours = 12;
}
var CurTime = hours + ":" + minutes + " " + suffix ;
player.SetVar("TimeValue",CurTime);
Always Welcome
I have a question--where in Storyline do you place these codes? Do you create a separate variable for each line of code? Anyone have a screengrab of what it looks like?
You add a Excute Javascript trigger to an object, e.g. a button and then place the JavaScript inside the JavaScript window. See below a screenshot of a button that when clicked executes the date-related JavaScript listed in this thread. You can also place your code into an external file and call that function from the trigger.
Sorry, forgot to include this link to some samples and demos that may be helpful: http://www.articulate.com/support/storyline/javascript-best-practices-and-examples
Hi Pam,
I just wanted to welcome you to the E-Learning Heroes community! It looks like Michael has you covered here but if you need anything else just let us know!
Hi Ashley, thank you; I'm happy to be here! Michael, thanks for answering my post so quickly. I'm traveling now, but I'll test drive it as soon as I get in.
Pam
Well we're happy to have you!
Hi, this is a great solution. I'm no expect in Javascript so I'm struggling to work this out. I would like to know what the code is to display the date 3 months ago. So the current date minus 3 months.
Any help much appreciated.
Thanks
Phil
Hi Phil,
This thread is a bit older so I"m not certain that folks are still subscribed, but you may want to use the "contact me" feature on their profiles to reach out direcly about how they may have accomplished this.
Hi Ashley,
It's okay I think I have figured it out.
I changed the +1 value on the month to -1 instead and that brings me back to January which is correct :)
var month = currentDate.getMonth() - 1
Glad you were able to figure something out Phil and thanks for sharing it here as I know there are often users who are looking for Javascript solutions!
I have actually found a flaw in my code. If I set my system time back to January 10th and run the script again, the date is displayed as 10/-1/15 rather than 10/10/14 I guess it's doing this because changing the month variable will not automatically change the year.
Any suggestions?
I am not equipped to help with Javascript issues, so I'll defer to the community on this set up.
I had issues getting this code to work, so I (not having any Javascripting experience) tinkered around with it until it did. Here is the code that does work which I have setup via two javascript triggers:
Trigger 1
var currentDate = new Date()
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear();
var player = GetPlayer();
var newName = month + "/" + day + "/" +year
player.SetVar("DateValue", newName);
Trigger 2
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()
if (hours <12)
var suffix = "AM";
if (hours >= 12)
var suffix = "PM";
var CurTime = hours+":"+minutes+":"+seconds+" "+suffix;
player.SetVar("TimeValue",CurTime);
To get this to work in Storyline, first setup two text variables, "DateValue", and "TimeValue". Then on your slide or master slide, setup a text box or object and then insert two references for DateValue and TimeValue. I simply set the triggers to execute Javascript when timeline starts.
You can modify the code easily by modifying the varCurTime, or var newName lines. I really am not sure what that nested statement in the time code was about in the original code but I could not get it to work.
Test file attached.
Thanks David! works perfectly
Glad this thread helped you out Elaine!
In JavaScript you can create date & time objects using the Date constructor. Then object can use its own methods for different computations afterward.
There are number of ways in which you can create the date object via Date constructor.
Example
var today_date_time = new Date(); // Get today's date and time
var old_date = new Date(2010, 4, 1); // Date Literal
var old_date_time = new Date(2010, 0, 1, 18, 10, 30); // Date literal + time
var elapsed = today_date_time - old_date ; // Get the interval after subtraction in milliseconds
// Methodsold_date.getFullYear() // get year
old_date.getMonth() // get month number (starting from zero)
old_date.getDate() // get day
old_date.getDay() // get day of week (0-6) Starting from Sunday: 0
old_date.getHours() // get local time
old_date.getUTCHours() // get time in UTC
See working example at:
http://www.thesstech.com/javascript/date-time
Thanks Sohail for sharing that here and providing some code for folks to use.
Huge help, thank you!
Glad to see that this thread was able to assist you as well Jill :)