0

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?

0

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

@user1157541 Always happy to help. Are you also working with jQuery mobile
1

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

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.