PDA

View Full Version : a 24 Hour clock script



booker4906
03-06-2007, 09:19 AM
Live Date Script

http://dynamicdrive.com/dynamicindex6/clock3.htm

Hello. I am trying to use this script for a live date/time on a website. However, i need a 24 hour clock and not AM/PM. Would you be able to provide me the change in the script for this to happen? Any help would be much appraciated! Thanks for this site.

jeff

codeexploiter
03-06-2007, 09:36 AM
I've made two small changes (commented some code) inorder to achieve what you want on the original Live Date Script. My modifications are in blue color in the below mentioned source.

Try the following code instead of the original one.



<script>

/*
Live Date Script-
&#169; Dynamic Drive (www.dynamicdrive.com)
For full source code, installation instructions, 100's more DHTML scripts, and Terms Of Use,
visit http://www.dynamicdrive.com
*/


var dayarray=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday")
var montharray=new Array("January","February","March","April","May","June","July","August","September","October","November","December")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
/*var dn="AM"
if (hours>=12)
dn="PM"
if (hours>12){
hours=hours-12
}*/

var dn = "";

/*if (hours==0)
hours=12*/

if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
//change font size here
var cdate="<small><font color='000000' face='Arial'><b>"+dayarray[day]+", "+montharray[month]+" "+daym+", "+year+" "+hours+":"+minutes+":"+seconds+" "+dn
+"</b></font></small>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}

</script>
<span id="clock"></span>

booker4906
03-06-2007, 09:58 AM
The code worked perfectly. Thank you so much for that!!!

sincerely,

Jeff

otlyn
04-13-2014, 06:12 PM
I'm very new to javascript, and was wondering if anyone could help me add open and close times to this. Say the business is open from 8:00 to 18:00 on Monday, Tuesday - Friday it's something else, Saturday is 10:00 to 18:00 and Sunday is closed.

Is it possible to add these details? And have the script return "Open" or "Closed" respectively?

If someone could just point me in the right direction, I'd be so grateful.

-Ot

vwphillips
04-14-2014, 11:14 AM
<!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" xml:lang="en" lang="en">

<head>
<title></title>
</head>

<body onload="goforit('clock');">
<script>

/*
Live Date Script-
Dynamic Drive (www.dynamicdrive.com)
For full source code, installation instructions, 100's more DHTML scripts, and Terms Of Use,
visit http://www.dynamicdrive.com
*/


var dayarray=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var montharray=new Array('January','February','March','April','May','June','July','August','September','October','November','December');

var opening=[];
opening['Sunday']=[];
opening['Monday']=['12:12','17:30'];
opening['Tuesday']=['9','17:30'];
opening['Wednesday']=['9','17:30'];
opening['Thursday']=['9','17:30'];
opening['Friday']=['9','17:30'];
opening['Saturday']=['9','12:30'];

function getthedate(){
var n=new Date(),h=n.getHours(),d=dayarray[n.getDay()],m=n.getMinutes(),a=opening[d],sm,fm;
if (a&&typeof(a[0])=='string'){
sm=a[0].split(':');
sm=(isFinite(sm[1]*1)?sm[1]*1:0)+(isFinite(sm[0]*1)?sm[0]*60:0);
}
if (a&&typeof(a[1])=='string'){
fm=a[1].split(':');
fm=(isFinite(fm[1]*1)?fm[1]*1:0)+(isFinite(fm[0]*1)?fm[0]*60:0);
}
d=d+', '+montharray[n.getMonth()]+' '+Add0(n.getDate())+', '+n.getFullYear()+' '+Add0(h-(h>12?12:0))+':'+Add0(m)+':'+Add0(n.getSeconds())+' '+(ap=h>12?'PM':'AM')+' '+(sm&&fm&&sm<=h*60+m&&h*60+m<fm?'OPEN':'CLOSED')
if (document.getElementById){
document.getElementById('clock').innerHTML=d
}

else
document.write(d)
}

if (!document.all&&!document.getElementById)
getthedate()

function goforit(id){
if (document.getElementById(id))
setInterval('getthedate()',1000)
}

function Add0(n){
return n<10?'0'+n:n;
}

</script>
<span id="clock"></span>

</body>

</html>