Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
Testing for Zeros in a date value
Old 03-27-2009, 10:39 AM Testing for Zeros in a date value
Novice Talker

Posts: 6
Name: Martin
Location: Bromsgrove, England UK
Trades: 0
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>");
zxnet2k is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-27-2009, 12:15 PM Re: Testing for Zeros in a date value
SolidEagle's Avatar
Experienced Talker

Posts: 30
Name: Arjan Nijmeijer
Location: Stadskanaal
Trades: 0
why don't you check:

if (now.getMinutes() < 10)
{
minutes = ("0" + minutes);
}

cause you'll get this problem only with 1 2 3 4 5 6 7 8 9
__________________
The Eagle Soars Above You!

Please login or register to view this content. Registration is FREE
SolidEagle is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Testing for Zeros in a date value
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.39124 seconds with 12 queries