JavaScript devuelve la hora, minuto y segundo actuales

Reclamación:

Encapsular una función para devolver el
formato de función de hora, minuto y segundo actual08:08:08

Código:

<!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>

Resultado de salida:

Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/Jack_lzx/article/details/109241804
Recomendado
Clasificación