I need help with the following code. I am trying to show the day of the week together with the day, month and year
var currentDate = new Date()
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday",
"Thursday","Friday","Saturday")
var now
var ken = dayNames[now.getDay()]
var day = currentDate.getDate()
var month = currentDate.getMonth() + 1
var year = currentDate.getFullYear()
document.write("<b>" ken +" " + day + "/" + month + "/" + year + "</b>")
<script>
var currentDate = new Date();
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var now = new Date();
var ken = dayNames[now.getDay()];
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
document.write("<b>" + ken +" " + day + "/" + month + "/" + year + "</b>");
</script>
The problems with your code:
1- lack of semi-colons. Yes, your code will work in most browser, but you should use them anyway.
2- "var now" but you never say what "now" is. Since "currentDate" is already today, you really don't need a variable for "now", but if you want it, you need to say "var now = new Date()".
3- Missing a "+" in your document.write line here: "<b>" ken +
Fixed code without the superfluous "now" variable.
Code:
<script>
var currentDate = new Date();
var dayNames = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var ken = dayNames[currentDate.getDay()];
var day = currentDate.getDate();
var month = currentDate.getMonth() + 1;
var year = currentDate.getFullYear();
document.write("<b>" + ken +" " + day + "/" + month + "/" + year + "</b>");
</script>
1) The semi-colon at the end of a line is optional according to the definition of the language. It's not browser dependent and you can use it or not as you like. I recommend omitting it whenever possible so as to irk certain people.
2) The "type" attribute, however, is required with the SCRIPT element.
3) Document.write takes a variable number of arguments and concatenates them. So you could omit the "+" things and use document.write("<b>", ken, " ", day, "/", month, "/", year, "</b>").
4) Except that you shouldn't use the B element here nor should you use document.write. Best to use STRONG and the DOM.
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
I also tried omitting the "+" and that also worked fine. Same is true with omitting semicolons. I did not understand the B element, but since i am a biginner, i will come to it as i work more.
Bookmarks