Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post

    change time format, display time as ??:??:??

    basically I have this script function that adds up all the time values in this form and out puts the total time at the end.

    Although At the moment when the total time goes down to single figures it doesnt disply a "0" infront of it like when the total time is "2:3:0" I would like it to be like this "02:03:00" instead

    So is there anyway of editing the script below so that it is in that format

    PHP Code:
            <script type="text/javascript">
                function 
    makeTotal()
                {
                    var list = 
    document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input");
                    var 
    total 0;

                    for(var 
    22< list.lengthi+=4s++) {
                        
    total += Number(list[i++].value) * 60 Number(list[i++].value);
                        var 
    time = new Date(total 1000); 
                        
    document.getElementById("result"+s).value time.getUTCHours() + ":" time.getUTCMinutes() + ":" time.getUTCSeconds(); 
                    }

                    var 
    time = new Date(total 1000);
                    var 
    result document.getElementById("result");
                    
    result.firstChild.nodeValue time.getUTCHours() + ":" time.getUTCMinutes() + ":" time.getUTCSeconds();
                    
    result.style.display "";
                    
                }
            
    </script> 
    Thanks

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Just check to see if the number is less than 10, and if it is, then append a zero on the front.

  • #3
    New Coder
    Join Date
    Jan 2005
    Location
    England
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    like this

    Like this:
    Code:
    if (hours<10)
    hours="0"+hours
    Congratulations on discovering the hidden sig! Click here to claim your prize!

  • #4
    Regular Coder
    Join Date
    Aug 2003
    Location
    scotland, edinburgh
    Posts
    176
    Thanks
    5
    Thanked 1 Time in 1 Post
    thanks for the advice i was able to do this

    PHP Code:
    <script type="text/javascript">
                function 
    makeTotal()
                {
                    var list = 
    document.getElementsByTagName("form")[0].getElementsByTagName("table")[0].getElementsByTagName("input");
                    var 
    total 0;
                    var 
    hours 0;
                    var 
    minutes 0;
                    var 
    seconds 0;

                    for(var 
    22< list.lengthi+=4s++) {
                        
    total += Number(list[i++].value) * 60 Number(list[i++].value);
                        var 
    time = new Date(total 1000); 
                        
    document.getElementById("result"+s).value time.getUTCHours() + ":" time.getUTCMinutes() + ":" time.getUTCSeconds(); 
                    }

                    var 
    time = new Date(total 1000);
                    
                    if (
    time.getUTCHours()<10){
                    
    hours="0"+time.getUTCHours();
                    }else{
                    
    hours=time.getUTCHours();
                    }
                    
                    if (
    time.getUTCMinutes()<10){
                    
    minutes="0"+time.getUTCMinutes();
                    }else{
                    
    minutes=time.getUTCMinutes();
                    }
                    
                    if (
    time.getUTCSeconds()<10){
                    
    seconds="0"+time.getUTCSeconds();
                    }else{
                    
    seconds=time.getUTCSeconds();
                    }
                    
                    var 
    result document.getElementById("result");
                    
    result.firstChild.nodeValue hours ":" minutes ":" seconds;
                    
    result.style.display "";
                    
                }
            
    </script> 


  •  

    Posting Permissions

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