JavaScript returns the current hour, minute and second

Claim:

Encapsulate a function to return the current hour, minute, and second function
Format08:08:08

Code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function getTimer() {
     
     
            var time = new Date();
            var h = time.getHours();
            h = h < 10 ? '0' + h : h;
            var m = time.getMinutes();
            m = m < 10 ? '0' + m : m;
            var s = time.getSeconds();
            s = s < 10 ? '0' + s : s;
            return h + ':' + m + ':' + s;
        }
        console.log(getTimer());
    </script>
</body>

</html>

Output result:

Insert picture description here

Guess you like

Origin blog.csdn.net/Jack_lzx/article/details/109241804