Forum Discussion
Date Format for System Date
Thank you Miriam, Owen and Katarina. The above code has been a great help.
If I am wanting to display a previous day & date (say 10 days ago) can I use the above code?
It seems to work fine if I modify the code to the below, if the date stays within the current month:
var weekDay = dayNames[d.getDay()-10];
var dd= [d.getDate()-10];
However if this changes the date to the previous month it does not work..
e.g. today is 'Thursday 7 May', -10 days I want the date to display as 'Tuesday 28 April'. Instead this displays as 'undefined -3 May'.
Can anyone suggest how to adjust Owen's/Katrina's code for this? Or do I need to use a js Calendar function instead? Thank you!
You actually need to change the date in the date variable before you pull the other information.
To do this, add this line in the code right after you pull the current date:
d.setDate(d.getDate() - 10);
So your 1st three lines of code should now look like this:
var d = new Date();
d.setDate(d.getDate() - 10);
var yyyy = d.getFullYear();
Basically, setDate is a JavaScript date function that lets you choose the exact date you want to work with. In this case, we are saying we want the date that is today's date minus 10 days. Once we have that as our base date, all of the other date "get" functions are getting the information from the date WE set.
- ColinTudehope5 years agoCommunity Member
Thank you so much Owen, that works beautifully!
- OwenHolt5 years agoSuper Hero
No problem. Note that you can use that same logic to create a course license expiration.
- Create a variable in StoryLine to hold a base date in mm/dd/yyyy format.
- Use JavaScript to pull the SL variable and store it in a JS variable as a date object using the setDate function.
- Pull the current date.
- Do some math to calculate how many days have passed from the base date to the current.
- Perform some action in SL based on the number of days that have passed.
See an example here (.story file in the resources tab): LINK
- ColinTudehope5 years agoCommunity Member
Thanks again Owen! That could definitely be useful on an upcoming project.