1

My time is updating in realtime with the following function:

    var nIntervId;

    function updateTime() {
        nIntervId = setInterval(flashTime, 1000*2);
    }

    function flashTime() {
        var now = new Date();
        var h = now.getHours();
        var m = now.getMinutes();
        var s = now.getSeconds();
        var time = h + ':' + m;
        $('.time').html(time);
    }

    $(function() {
        updateTime();
    });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

 <span class="time" style="margin-left:20px"></span>

Problem ist, that it is displaying 11:9 instead if 11:09

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101
peace_love
  • 6,229
  • 11
  • 69
  • 157

1 Answers1

2

Problem ist, that it is displaying 11:9 instead if 11:09

You could add a condition like :

var m = now.getMinutes() < 10? '0'+now.getMinutes(): now.getMinutes();

That will add the zero if the minutes are lower than 10.

Zakaria Acharki
  • 66,747
  • 15
  • 75
  • 101