Your current weekDay()
function will return the current day of the week, but your other code calls weekDay()
and doesn’t do anything with its return value.
You need to use the value together with some string concatenation:
var today = weekDay();
document.write("<iframe src="" + today + "".htm'></iframe>");
Or just:
document.write("<iframe src="" + weekDay() + "".htm'></iframe>");
“And here is what I have tried in my HTML page: (tried using the var wdName??)”
You can’t use wdName
from outside the function, because it is a local variable accessible only inside the function. As it should be, given that (as explained above) the function returns the string you want. Note that your variable thisDate
will be global because it is not declared with var
. It is best practice to declare all variables with var
, which in your case would make thisDate
a local variable in that function. Also it is considered best practice to declare arrays with []
rather than new Array()
, so:
function weekDay(){
var thisDate = new Date();
var thisWDay=thisDate.getDay();
var wdName = ["sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday"];
return wdName[thisWDay];
}
Which simplifies to:
function weekDay(){
return ["sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday"][ (new Date()).getDay() ];
}
5
solved Javascript days of the week [closed]