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);
- DanielRosen1Community Member
Hi Miriam,
While researching something for a colleague, I ran across your post. Here's the modified code that will allow you to display the date the way you want:
var currentTime = new Date();
var monthArray = new Array ("January","February","March","April","May","June","July","August","September","October","November","December");
var month = currentTime.getMonth();
var theMonth = monthArray[month];
var day = currentTime.getDate();
var year = currentTime.getFullYear();
var dateString= day + " " + theMonth + " " + year;
var player = GetPlayer();
player.SetVar("SystemDate",dateString); - OwenHoltSuper Hero
In JavaScript, "Date" is an object with lots of information in it but sometimes, you need to give it a little bit more information to get the format you want.
var month = currentTime.getMonth() + 1 returns a number from 1 to 12. Months are actually captured as 0 through 11 in the date object which is why the code has "+ 1" at the end. To change this to the actual month name, you need to create an array of month names and then pick the one you want based on the value in your month variable. Keep in mind, arrays are numbered starting at zero, so you can actually drop the "+ 1" from the code. In your code above, replace "var month = currentTime.getMonth() + 1" with the following.
var month = currentTime.getMonth();
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var currentMonth = monthNames[month];var day = currentTime.getDate() returns the actual date but as a number. Similar to the month names, you will need to create an array of days formatted how you want them to be displayed. Because this is the actual date, it will not align to the formatted dates in your array since the array starts at 0 and the 1st day of the month is "1". To create the alignment, you will need to subtract one from your day. In your code above replace "var day = currentTime.getDate()" with the following:
var day = currentTime.getDate();
var formatDay = ["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 todaysDate = formatDay[day-1];You do not need to change the year but you will need to update your date string. Replace "var dateString=month + "/" + day + "/" + year" with the following:
var dateString = todaysDate + " day of " + currentMonth + ", " + year;
This will return something like "22nd day of December, 2020".
- MiriamKrajewskiCommunity Member
Thank you thank you thank you Daniel! That works perfectly now. I'm so excited, thank you!
- DanielRosen1Community Member
Happy to help.
- MikeWohlwendCommunity Member
Miriam Krajewski said:
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);
Miriam, for future reference, the line with "var dateString" could probably have its later order shifted; looks like it is in this line that the order of month / day / year is established.If this script worked before, changing that line to:
var dateString=day + "/" + month + "/" + year
should also display it ordered as you want to see it.
So the nice thing is, sometimes code can be as comprehensible as that!
- KristinCarra-2cCommunity Member
Hi Kristin!
This thread is a bit dated and I'm not sure that the users here are still subscribed to the thread. JavaScript is not something that is supported by Articulate and I'm sure not an expert at it. Hopefully someone in the community will be able to chime in and assist you here.
- JiayuSunCommunity Member
Hi, i would also like to know whether it's possible to make date format as DDMMYYYY, thank you.
Hi Jiayu,
This discussion is still quite a bit older and I haven't seen that many other similar ones. Where are you looking to change the date format? If you'd like to update this in your Learning Management System (LMS) you may also want to reach out to your LMS admins to see if they can help!
- JiayuSunCommunity Member
Hi Ashley,
i want to show the date on one of the slide and by using javascript code mentioned above, the date shows like DD.M.YYYY, not DD.MM.YYYY, for example, 29.1.2018 instead of 29.01.2018
Ah, thanks Jiayu! I'll have to defer to the community to help with Javascript!