www.webdeveloper.com
Results 1 to 4 of 4

Thread: How to convert decimal time value to hh:mm?

  1. #1
    Join Date
    Sep 2009
    Posts
    3

    Question How to convert decimal time value to hh:mm?

    I have a script which returns a decimal value to represent time. I need to convert the decimal time value to hh:mm (i.e: 90 minutes decimal value 1.5 should be represented as 1:30. Having trouble figuring this out. Attached is my current script.

    Code:
    <script>
    function Convert() {
        var minutes = document.getElementById("sum").value;
        var hours = minutes / 60;
        document.getElementById('total').value = hours;
        document.getElementById('sum').value = minutes;
    var convert = function (sum) {
        return Math.floor(sum/60) + ":" + (sum-Math.floor(sum/60)*60);
    }
    
     
    </script>

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415

    Lightbulb

    Something like this ...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
     <input id="HrMin" value="1.5"> Decimal Hours and Minutes <br>
     <button
     onclick="document.getElementById('HHMM').value=convertToHHMM(document.getElementById('HrMin').value)">
     Convert to HH:MM</button>
     Results <input id="HHMM" value="">
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?273645-How-to-convert-decimal-time-value-to-hh-mm&daysprune=30
    
    function convertToHHMM(info) {
      var hrs = parseInt(Number(info));
      var min = Math.round((Number(info)-hrs) * 60);
      return hrs+':'+min;
    }
    </script>
    
    </body>
    </html>

  3. #3
    Join Date
    Sep 2009
    Posts
    3
    That works ..... Thank You





    Quote Originally Posted by JMRKER View Post
    Something like this ...
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title> Untitled </title>
    </head>
    <body>
     <input id="HrMin" value="1.5"> Decimal Hours and Minutes <br>
     <button
     onclick="document.getElementById('HHMM').value=convertToHHMM(document.getElementById('HrMin').value)">
     Convert to HH:MM</button>
     Results <input id="HHMM" value="">
    
    <script type="text/javascript">
    // From: http://www.webdeveloper.com/forum/showthread.php?273645-How-to-convert-decimal-time-value-to-hh-mm&daysprune=30
    
    function convertToHHMM(info) {
      var hrs = parseInt(Number(info));
      var min = Math.round((Number(info)-hrs) * 60);
      return hrs+':'+min;
    }
    </script>
    
    </body>
    </html>

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,415
    You're most welcome.
    Happy to help.
    Good Luck!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles