I just need to create a date object for my final and it isn\'t working. My codin
ID: 3635284 • Letter: I
Question
I just need to create a date object for my final and it isn't working. My coding is:function nowTime
{
var today = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
currentHours = (currentHours > 12) ? currentHours -12: currentHours;
currentHours = (currentHours == 0 ? 12 :currentHours;
currentMinutes = (currentMinutes < 10) ? "0"+currentMinutes: currentMinutes;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
document.time.write(currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay);
}
and this is located in my head section of the HTML coding.
Now I need to place that into my body area, but it won't work! Do i need to call the function in my body? and if yes, how? Please be specific because I am having a hard time with the time stuff.
My entire coding is:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Home Page</title>
<link href="styles1.css" rel="stylesheet" type="text/css" />
<script>
function changeImage()
{
document.images["homepicture"].src="z1.jpg"
return true;
}
function changeImageBack()
{
document.images["homepicture"].src="homepicture.jpg"
return true;
}
function nowTime
{
var today = new Date();
var currentHours = currentTime.getHours();
var currentMinutes = currentTime.getMinutes();
var currentSeconds = currentTime.getSeconds();
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
currentHours = (currentHours > 12) ? currentHours -12: currentHours;
currentHours = (currentHours == 0 ? 12 :currentHours;
currentMinutes = (currentMinutes < 10) ? "0"+currentMinutes: currentMinutes;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
document.time.write(currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay);
}
</script>
</head>
<body background="b.jpg">
<p> It is now <a id="time"></a></p>
<p id="key">My name is and welcome to my page!</p>
<hr/>
<p id="fleft">
<a href="interest.htm"><b>Interests</b></a>
<br/>
<a href="resume.htm"><b>Resume</b></a>
<br/>
<a href="form.htm"><b>Comment?!</b></a>
<br/>
</p>
<a href="" ><img src="homepicture.jpg" alt="picture of me" id="homepicture" name="homepicture" width="360" height="241"/></a>
<p id="fright">
<a href="http://twitter.com/Ci"><b>Twitter</b></a>
<br/>
<a href="http://kongregate.com/games/FliplineStudios/papas-taco-mia"><b>Board of my Site?</b></a>
<br/>
<a href="http://youtube.com/watch?v=j3NgTqppHec"><b>Roll Tide!</b></a>
<br/>
</p>
</body>
</html>
Explanation / Answer
function nowTime() {
var today = new Date();
var currentHours = today.getHours();
var currentMinutes = today.getMinutes();
var currentSeconds = today.getSeconds();
var timeOfDay = (currentHours < 12) ? "AM" : "PM";
currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
currentHours = (currentHours == 0) ? 12 : currentHours;
currentMinutes = (currentMinutes < 10) ? "0"+currentMinutes: currentMinutes;
var currentTimeString = currentHours + ":" + currentMinutes + ":" + currentSeconds + " " + timeOfDay;
document.getElementById('time').innerHTML = currentTimeString;
}
This is the correct nowTime() function.
Now you must call this when the whole document is loaded.
For that, you must write an onLoad attribute to <body> as:
<body background="b.jpg">
Please rate :)