Posts: 6
Name: Martin
Location: Bromsgrove, England UK
|
Hi everyone,
This place helped me out with a problem before so I thought I would return and hope it can happen again
Basically I have a simple HTML page and a JavaScript that is printing the current time and a message based on the hour. The script works fine, apart from some validation I am trying to put in. If the current time was 12:04:56, then it would display as 12:4:56, omitting the zero. To try and counteract this I have put in some code which attempts to search for a zero the minutes variable and if it finds one it puts a string "0" value in front of the number. Thats the idea, the code is below, Can anyone help??
Happy to accept all criticisms, coding advice and ideas; as well as a solution if possible!
HTML
Code:
<html>
<head><title>Displaying Date and Time</title></head>
<body>
<h1>Current Date and Time</h1>
<p>
<script language="JavaScript" type="text/javascript" src="timegreet.js">
</script>
</p>
</body>
</html>
JavaScript
Code:
var testval;
// Get the current date
now = new Date();
//Split into hours mins secs
hours = now.getHours();
minutes = now.getMinutes();
seconds = now.getSeconds();
//Testing for zeros
testval = minutes.indexOf("0");
if (testval == 0)
{
minutes = ("0" + minutes);
}
//Display the time
document.write("<h2>");
document.write(hours + ":" + minutes + ":" + seconds);
document.write("</h2>");
//Display a greeting
document.write("<p>");
if (hours < 10) document.write("Good Morning!");
else if (hours >= 14 && hours <= 17) document.write("Good Afternoon");
else if (hours > 17) document.write("Good Evening!");
else document.write("Good Day!");
document.write("</p>");
|