Bryan
Bryan

Reputation: 3009

Javascript date array

Having some trouble with a Javascript function. Here is my code:

function date(){
  var d = new Date();
  var day = d.getDay();
  var month = d.getMonth() + 1;
  var date = d.getDate();
  var year = d.getFullYear();
  var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");


  document.getElementById("footer").innerHTML = day[days] + " " + month + "/" + date + "/" +  year;
}

This function is returning "undefined 3/5/2013", but should instead be returning "Tuesday 3/5/2013." Is there an error in my logic? Can anyone help me find my error?

Upvotes: 0

Views: 946

Answers (3)

user1999257
user1999257

Reputation:

Use this code

document.getElementById("footer").innerHTML =days[day] + " " + month + "/" + date + "/" +  year;

By mistake you have used

day[days]

instead of

days[day]

var day = d.getDay();

This method will return integer value for the day which you have to use in days[] array as index like days[d.getDay()] and you are doing d.getDay()[days] Which is not correct

Upvotes: 1

Aashray
Aashray

Reputation: 2753

days is the array and not day. You are calling day[days]. it should be days[day].

Upvotes: 1

Arun P Johny
Arun P Johny

Reputation: 388316

You code should be

document.getElementById("footer").innerHTML = days[day] + " " + month + "/" + date + "/" + year;

days is the array where day is the day of the week variable, you swapped those two variables

Upvotes: 1

Related Questions