Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Endless For Loop?

Status
Not open for further replies.

NJDrew

Technical User
Dec 21, 2001
31
US
Hi, Im trying toi convert the 0-23 resut given back from the getHours method into 1-12. I used a for loop, but it seems to my && operater is not working, because seems to Dead cycle when i run it. What am I doing wrong? Is there a simpler way to do thins? Thanks, Drew

My code:
Code:
var curdate = new Date();
var hours = curdate.getHours();
var x = 0;
if (hours == 0)
hours = 1;
else
for (x = 1; hours >= 13 &&  hours <= 23; x++)
var hour = hours - x;

document.write(hour + &quot;<BR>&quot;);

&quot;Mistakes are the portals of discovery&quot;
James Joyce
 
Let me know if I've got it all wrong, but if you are simply trying to convert a 24hr time to an am/pm format wouldn't it be simpler to do it this way:

suffix = 'am';
if(hours == 0){
hours = 12;
}
if(hours > 12){
hours = hours - 12
suffix = 'pm';
}

timestr = hours + &quot;:&quot; + minutes + &quot;:&quot; + seconds + suffix;
 
dwarfthrower, your 100% right. Your solution is so simple it's genius. I would still like to know why that && isn't working, however I will use your solution. Thank you very much.

&quot;Mistakes are the portals of discovery&quot;
James Joyce
 
The && is working fine, but you never modify the value of &quot;hours&quot; (only the one of &quot;hour&quot;), so the test in the loop will always return true.



vlad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top