Find jobs - PT Cruiser - Music Store - Debt Consolidation - Wordpress Themes

PDA

View Full Version : Javascript 24hr-time issue


SuPrAiCeR69
May 6th 2009, 7:33 pm
I looked around for a 24hr JS time script and found the following from http://www.crays.com/jsc/jsC4Udate.htm:

Time in 24-hr format

<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var curDateTime = new Date()
var curHour = curDateTime.getHours()
var curMin = curDateTime.getMinutes()
var curSec = curDateTime.getSeconds()
var curTime =
((curHour < 10) ? "0" : "") + curHour + ":"
+ ((curMin < 10) ? "0" : "") + curMin + ":"
+ ((curSec < 10) ? "0" : "") + curSec
document.write(curTime)
//-->
</SCRIPT>

After testing it, I realised at 12pm the time goes to "00:00:00" instead of staying at 12. At 12am the time goes to "00:00:00" which is fine. How can I fix this or is there a better written script around?

SuPrAiCeR69
May 6th 2009, 7:42 pm
Another questions is how to code the following;

If the time is between 7pm and 7am
do this
else
If the time is between 7am and 7pm
do this

Martin K
May 14th 2009, 9:28 am
Hi SuPrAiCeR69,
I hope this will work:

<SCRIPT Language="JavaScript">
<!-- hide from old browsers
var curDateTime = new Date()
var curHour = curDateTime.getHours()
if (curHour >= 24) curHour -= 24
if (curHour < 0) curHour += 24
var curMin = curDateTime.getMinutes()
var curSec = curDateTime.getSeconds()
var curTime =
((curHour < 10) ? "0" : "") + curHour + ":"
+ ((curMin < 10) ? "0" : "") + curMin + ":"
+ ((curSec < 10) ? "0" : "") + curSec
document.write(curTime)
document.write("<br />")
if (curHour>=19 || curHour<7){
document.write("The time is between 7pm and 7am. It's night")
}
else{
document.write("The time is between 7am and 7pm. It's day")
}
//-->
</SCRIPT>


Martin