Click here to Skip to main content
14,330,663 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to display the clock in string instead of numbers .
for example :
10:15:11 should be like
ten :fifty min.eleven sec

What I have tried:

body>
    <div id ="clock">10.10.01</div>

    <script>
      setInterval(GetClock,50);
     function GetClock (){
       var time = new Date();
       var hour = time.getHours();
       var mintue = time.getMinutes();
       var second = time.getSeconds();
       document.getElementById("clock").innerHTML = hour + ':' + mintue + ':' + second;
     }
   
      
   </script>
Posted
Updated 2 days ago
Rate this:
Please Sign up or sign in to vote.

Solution 1

You might find this page Converting Numbers Into Words Using JavaScript[^] useful.
   
Comments
Maciej Los 2-Oct-19 6:37am
   
5ed!
CPallini 2-Oct-19 15:36pm
   
Thank you, Maciej!
Rate this:
Please Sign up or sign in to vote.

Solution 2

<html>
<body>

<h2>My First JavaScript</h2>

 <div id ="clock">10.10.01</div>

<script>

setInterval(GetClock,500);
debugger;
     function GetClock (){
       var time = new Date();
       var hour = convertNumberToWords(time.getHours());
       var minute = convertNumberToWords(time.getMinutes());
       var second = convertNumberToWords(time.getSeconds());
       
       document.getElementById("clock").innerHTML =  hour +  ' Hour ' + minute + ' Min ' + second + ' Sec ' ;
     }

function convertNumberToWords(amount) {
    var words = new Array();
    words[0] = 'Zero';
    words[1] = 'One';
    words[2] = 'Two';
    words[3] = 'Three';
    words[4] = 'Four';
    words[5] = 'Five';
    words[6] = 'Six';
    words[7] = 'Seven';
    words[8] = 'Eight';
    words[9] = 'Nine';
    words[10] = 'Ten';
    words[11] = 'Eleven';
    words[12] = 'Twelve';
    words[13] = 'Thirteen';
    words[14] = 'Fourteen';
    words[15] = 'Fifteen';
    words[16] = 'Sixteen';
    words[17] = 'Seventeen';
    words[18] = 'Eighteen';
    words[19] = 'Nineteen';
    words[20] = 'Twenty';
    words[30] = 'Thirty';
    words[40] = 'Forty';
    words[50] = 'Fifty';
    amount = amount.toString();
    var atemp = amount.split(".");
    var number = atemp[0].split(",").join("");
    var n_length = number.length;
    var words_string = "";
    if (n_length <= 9) {
        var n_array = new Array(0, 0, 0, 0, 0, 0, 0, 0, 0);
        var received_n_array = new Array();
        for (var i = 0; i < n_length; i++) {
            received_n_array[i] = number.substr(i, 1);
        }
        for (var i = 9 - n_length, j = 0; i < 9; i++, j++) {
            n_array[i] = received_n_array[j];
        }
        for (var i = 0, j = 1; i < 9; i++, j++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                if (n_array[i] == 1) {
                    n_array[j] = 10 + parseInt(n_array[j]);
                    n_array[i] = 0;
                }
            }
        }
        value = "";
        for (var i = 0; i < 9; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 7) {
                value = n_array[i] * 10;
            } else {
                value = n_array[i];
            }
            if (value != 0) {
                words_string += words[value] + " ";
            }
        }
        words_string = words_string.split("  ").join(" ");
    }
    return words_string;
}

</script>
</html>
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100