I am trying to display the text ‘Oct 09, 2012’. Instead it is not running the function and is displaying a lot of unnessecary date text. Does anyone know what I am doing wrong?
You can play with my jsfiddle… http://jsfiddle.net/UP3fd/
Here is the code…
var myDate = new Date();
convertDate(myDate);
myDate.setFullYear(2012, 9, 9);
document.write(myDate);
function convertDate(d) {
var day = d.getDate();
if (day < 10) {
day = "0" + day;
}
var year = d.getFullYear();
var month = d.getMonth();
var months=["Jan","Feb","Mar","Apr","May","June","July","Aug","Sep","Oct"," Nov","Dec"];
var currentMonth = months[month];
return (currentMonth + " " + day + ", " + year);
}
You are calling your function before you set your date, and you are not saving/outputting the return value anywhere.