Reputation: 23
So I'm completely new to HTML and programming. I'm trying to get a problem completed for school homework and I'm having a ton of trouble. I've been through the tutorial and googled quite a bit and I just can't seem to figure it out.
I have this function:
function showDate() {
thisDate = new Date();
var thisWDay=thisDate.getDay();
var thisDay=thisDate.getDate();
var thisMonth=thisDate.getMonth();
var thisYear=thisDate.getFullYear();
var mName = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October","November", "December");
var wdName = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
return wdName[thisWDay]+", "+mName[thisMonth]+" "+thisDay+", "+thisYear;
}
I'm required to display the output of this function in a web document. I'm following the tutorial but it doesn't seen to want to work. I know I'm doing something wrong. Here is what I have:
<script type="text/javascript">
function showDate();
document.write("Today is <b> " + showDate() + "</b>");
</script>
I know this is probably really simple but any push in the right direction would be greatly appreciated.
Upvotes: 2
Views: 53
Reputation: 45806
This code is invalid, so it's likely causing an error if you check your browser's developer console (usually opened by pressing F12), which is preventing the rest of the code from being executed.
function showDate();
This is likely causing your problem. I'm not sure what your intent is, but when you use the function
keyword, you're indicating that you're about to define a new function. You already have showDate
defined though; although you didn't include it in your second snippet.
Replace function showDate();
in your second snippet with the definition in the first snippet, and it should work. It should look like:
<script type="text/javascript">
function showDate() {
var thisDate = new Date();
var thisWDay=thisDate.getDay();
var thisDay=thisDate.getDate();
var thisMonth=thisDate.getMonth();
var thisYear=thisDate.getFullYear();
var mName = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October","November", "December");
var wdName = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
return wdName[thisWDay]+", "+mName[thisMonth]+" "+thisDay+", "+thisYear;
}
document.write("Today is <b> " + showDate() + "</b>");
</script>
Upvotes: 0
Reputation: 3974
Try this:
function showDate() {
var thisDate = new Date();
var thisWDay = thisDate.getDay();
var thisDay = thisDate.getDate();
var thisMonth = thisDate.getMonth();
var thisYear = thisDate.getFullYear();
var mName = new Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
var wdName = new Array("Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday");
return wdName[thisWDay] + ", " + mName[thisMonth] + " " + thisDay + ", " + thisYear;
}
You will have the set the variable after the function call.
<script type="text/javascript">
var dt = showDate();
document.write("Today is <b> " + dt + "</b>");
</script>
Upvotes: 2