Displaying the date from 3 months ago
Mar 10, 2015
Hi,
I have created a course which displays the current date on screen.
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=day + "/" + month + "/" + year
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
This works fine however I want the date to change to 3 months previous when the user clicks a button, so example the current date is: 10/03/15 and when the user clicks the button it changes to: 10/01/15.
I edited the code to - 1
rather than + 1
and this worked however if I set my system clock to 10/01/15 and ran the course again the output is displayed as "10/-1/15" instead of the correct date of "10/10/14"
Does anyone have any suggestions?
Thanks
Phil
16 Replies
Hello Phil,
I just wanted to share this other thread that discussed adding Dates in Storyline courses. And hopefully other members of the community will jump in with some assistance.
Hi Emily,
Thanks for your reply, hoping someone will be able to help me out with this.
http://stackoverflow.com/questions/7937233/how-do-i-calculate-the-date-in-javascript-three-months-prior-to-today
Thanks for sharing Jackson!
Hi Jackson,
Thanks for sharing the link. I found that same link yesterday and altered the code accordingly but I couldn't get it to work. I'm not sure what I did wrong...
can you post the code you altered?
var currentTime = new Date()
currentTime.setMonth(currentTime.getMonth() - 3 );
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=day + "/" + month + "/" + year
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
So the variable "d" used in the example I replaced with currentTime as they were both equalling "new Date()"
I'm no wizz at JS at all so I didn't expect it to work!
I just copied and pasted your code into a trigger and its working for me. I attached a .story file with the working trigger.
Test it out: http://s3.amazonaws.com/tempshare-stage.storyline.articulate.com/stp19g4s9mi4nk217gnt811tf9mvv4/story.html
Thanks Jackson, that worked a treat. I think the order in which the variables were called in yours were slightly different to mine which is why mine wasn't working.
Thanks again.
Hi Jackson, I wonder if you might be about to help me again? If I wanted to display the date from 3 and half months ago, how would I achieve this?
I tried to do this: currentTime.setMonth(currentTime.getMonth() - 3.5 );
And expectedly it didn't work. Any suggestions?
Thanks
Sussed it! I added this line above the command: currentTime.setDate(currentTime.getDate() - 14 );
Glad you got it working :)
Thanks for the update Phil. Glad you got this figured out :)
Hi Jackson
thank you very much for this download. I am really trying to understand what you've done here. For example I can see that you have 3 variables - date, systemDate and newDate. However you only reference date and systemDate. Why is this? What is the purpose of the newDate variable?
Also, in the date variable you have 3 lines of code at the end that correspond to month -3. Why is this?
I am looking to create 3 new dates:
1) 28 days from today
2) 7 days from today
3) 24 hours from now.
I just can't figure it out! I'd appreciate any help you or the community can provide!
Ginny
Hi Ginny!
This thread is a bit dated. I'm sure that Jackson is probably still subscribed to this thread as he's a pretty active community member, but just in case you are welcome to utilize the 'Contact Me' option on the user profile.
Hi Leslie
thanks for your suggestion. I've just taken your advice and contacted Jackson!
This discussion is closed. You can start a new discussion or contact Articulate Support.