Forum Discussion
Date Format for System Date
Hi there,
I followed the excellent advice and instructions from this thread for inserting a date into my Storyline
http://community.articulate.com/forums/p/12498/74794.aspx#74794 and I'm thrilled to say it's working just fine.
The date format is month/day/year but my course will be primarily used outside the US (where most countries record the date as day/month/year). I manged to change the order of the string instruction so that my output is now day/month/year but I would ideally prefer if the date came out as the word instead of the number. ie 15 January 2013 instead of either 1/15/13 or 15/1/13, as this would ensure the date was read properly globally.
I've tried searching the web for Javascript advice but I have no knowledge of JS and the advice is losing me completely! Can anyone suggest adjustment to the JS code that is recommended in the above thread, which is:
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);
- KatarinaKlein-7Community Member
Is there a code to format the system date as:
Day of the week, month date, year
(for example: Tuesday, March 5th, 2019)
- OwenHoltSuper Hero
Untested... but something like the following should get you the date string you are looking for.
var d = new Date();
var yyyy = d.getFullYear();
var month = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var currentMonth = month[d.getMonth()];
var dayNames = ['Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var weekDay = dayNames[d.getDay()];
var daySyntax = ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th','31st'];
var dd = daySyntax[d.getDate()-1];
var todaysDate = weekDay + ", " + currentMonth + " " + dd + ", " + yyyy
console.log(todaysDate);
GetPlayer().SetVar("Your Date Variable In StoryLine",todaysDate);- KatarinaKlein-7Community Member
Thank you for the quick reply!
After playing around with it for a bit, I was able to make it work how I needed by referencing %todaysDate% and executing the following JavaScript:
var d = new Date();
var yyyy = d.getFullYear();
var month = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var currentMonth = month[d.getMonth()];
var dayNames = ['Sunday','Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var weekDay = dayNames[d.getDay()];
var daySyntax = ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th','11th','12th','13th','14th','15th','16th','17th','18th','19th','20th','21st','22nd','23rd','24th','25th','26th','27th','28th','29th','30th','31st'];
var dd = daySyntax[d.getDate()-1];
var todaysDate = weekDay + ", " + currentMonth + " " + dd + ", " + yyyy
console.log(todaysDate);
var dateString=weekDay + ", " + currentMonth + " " + dd + ", " + yyyy
var player = GetPlayer();
player.SetVar("todaysDate",dateString);
- OwenHoltSuper Hero
Glad I could help.
- ColinTudehopeCommunity Member
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!
- OwenHoltSuper Hero
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.
- ColinTudehopeCommunity Member
Thank you so much Owen, that works beautifully!
- SharleneSteciukCommunity Member
Thank you!!! I am learning so much from everyone here and this is what I was able to gather from everyone here and resources elsewhere.
Javascript - using a variable to call the current date and an expiration date 180 days later
Code 1 System Date
var currentTime = new Date()
var month = currentTime.getMonth() + 1
if(month<10);
{
month='0' + month;
}
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=month + "/" + day + "/" + year
var player = GetPlayer();
player.SetVar("SystemDate",dateString);
Code 2 Expire Date - 180 days later
var someDate = new Date();
var numberOfDaysToAdd = 180;
someDate.setDate(someDate.getDate() + numberOfDaysToAdd);
var dd = someDate.getDate();
var mm = someDate.getMonth() + 1;
var y = someDate.getFullYear();
var dateString = mm + "/" + dd + "/" + y;
var player = GetPlayer();
player.SetVar("ExpireDate",dateString);
- MarioTaplin-187Community Member
Hello all,
I'm in need of serious help with this. I have a certificate of completion that reads as follows:
On this 1st day of December, 2020, John Doe has been certified....
I know I have to use a separate JS code for the date, month, and year but I'm having trouble formatting everything. Can someone let me know the JS code for the date? Then the month, then the year? I tried using the code below but I need each one formatted separately. Any help will be greatly appreciated.
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);