Results 1 to 5 of 5

Thread: a 24 Hour clock script

  1. #1
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default a 24 Hour clock script

    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

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    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.

    Code:
    <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>

  3. #3
    Join Date
    Mar 2007
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Thank You.

    The code worked perfectly. Thank you so much for that!!!

    sincerely,

    Jeff

  4. #4
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default adding Open / Close times

    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

  5. #5
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!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>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •