0
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.write(n + getDate + getFullYear);

I'm having trouble displaying the current date. Any help would be helpful.

2
  • getDate and getFullYear need to be accessed through the Date instance, d, and they are functions so they need to be invoked with (), i.e. d.getDate(). Also, don't forget about Array literals ['a', 'b', 'c'], they will save you a lot of typing. Commented Sep 5, 2015 at 21:08
  • As it stands, the current code will most likely throw a ReferenceError. You can see any error messages in your browser's Console (usually accessible by F12 or Ctrl+Shift+J). The message will give you some useful hints about what may be wrong and the line number (clicking the line number will take you to exactly where the error was thrown) Commented Sep 5, 2015 at 21:14

1 Answer 1

1

Looks like the problem is in your call to document.write. You need to call getDate and getFullYear on the date object you created (d). See below

var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.write(n + ', ' + d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear());

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

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.