Friday, 31 March 2017

How to print Datetime in JavaScript?

//Html Div
<div id='ct'></div>


//JavaScript Code
 <script type="text/javascript">
        function startTime() {
            var today = new Date();
            var h = today.getHours();
            var m = today.getMinutes();
            var s = today.getSeconds();
            m = checkTime(m);
            s = checkTime(s);
            var x1 = checkTime(today.getDate()) + "-" + checkTime(today.getMonth() + 1) + "-" +             checkTime(today.getFullYear());
            document.getElementById('ct').innerHTML = x1 + " " + h + " " + m + " " + s;
            var t = setTimeout(startTime, 500);
        }
        function checkTime(i) {
            if (i < 10) { i = "0" + i };  // add zero in front of numbers < 10
            return i;
        }
    </script>

No comments:

Post a Comment