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
Phil Eagles

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!

Ginny Franklin

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

This discussion is closed. You can start a new discussion or contact Articulate Support.