Javascript for Date

Aug 05, 2015

Hello everyone,

I'm currently creating a certificate page on my course and I want the days date to displayed like this:

5th August 2015

Could someone tell me what the JavaScript is in order to achieve this? 

I have managed to get the script for the date like this:

5 August 2015

But can't seem to work how I get "st" "nd" "rd" and "th"

 

Thanks

Jake

6 Replies
Jackson Hamner

If your code looks something like this:

var m_names = new Array("January", "February", "March", 
"April", "May", "June", "July", "August", "September",
"October", "November", "December");var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();//+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) { dd='0'+dd }
var date= m_names[mm]+' '+dd+', '+yyyy;

You can add it with these additional lines of code.

var m_names = new Array("January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December");var today = new Date();
var dd = today.getDate();
var mm = today.getMonth();//+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10) { dd='0'+dd }
var suffix = "th";
if(dd == 1){
suffix="st";
}
if(dd == 2){
suffix="nd";
}
if(dd == 3){
suffix="rd";
}
var date= m_names[mm] + ' ' + dd + suffix + ', ' + yyyy;

I havent tested it, but this should work. It can also be accomplished with switches rather than if/thens.

Let me know if it works, I hope this helps!

Mark Bennett

this accounts for the 11th, 21st, etc

var today = new Date();

var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
];

var s = ["th","st","nd","rd"];

var day = today.getDate();

var month = today.getMonth();

var year = today.getFullYear();

var v = day%100;

var dd = day+(s[(v-20)%10]||s[v]||s[0]);

var date = dd + " " + monthNames[month] + " " + year;

alert(date);
Jake Adams

This is the code I am using at the moment and its displaying the date like this

6 August 2015

var currentTime = new Date()

var month=new Array();
month[0]="January";
month[1]="February";
month[2]="March";
month[3]="April";
month[4]="May";
month[5]="June";
month[6]="July";
month[7]="August";
month[8]="September";
month[9]="October";
month[10]="November";
month[11]="December";
var monthSpelled = month[currentTime.getMonth()];
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var dateString=day + " " + monthSpelled + ", " + year
var player = GetPlayer();
player.SetVar("SystemDate",dateString);

 

SystemDate is the variable I'm using

 

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