How to display the time in JavaScript

Category:
日本語はこちら
<script>
    function digs(num) {
        return ( num < 10 )?num = "0" + num:num = num
    }
    function Watch() {
        var date = new Date();
        var nowHour = digs( date.getHours() );
        var nowMin = digs( date.getMinutes() );
        var nowSec = digs( date.getSeconds() );
        var msg = nowHour + ":" + nowMin + ":" + nowSec;
        document.getElementById("Watch").innerHTML = msg;
    }
    setInterval('Watch()',500);
</script>
<div id="Watch">00:00:00</div>

JavaScript’s date/time acquisition is special, and is in the form of a “get” for each.

In the sample program, the date/time information is updated every 500 ms, and the id is reflected in the Watch div.

The code is so simple that a detailed explanation is omitted.

The sample programs are released under the MIT License.
Please refer to the following terms of use before use.
Terms of Use of Source Code (Lang: Japanese)


Categories:


Categories


Recent Posts


Tags


CSS HTML JavaScript Web