O método é muito simples e o código é muito conciso. Basta apresentar o código
A cópia do código é a seguinte:
<! Doctype html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title> JS Obtenha a hora atual exibida na página </ititle>
<Cript>
window.onload = function () {
// O timer chama fndate () uma vez por segundo
setInterval (function () {
fndate ();
}, 1000);
}
// JS Obtenha o horário atual
função fndate () {
var odiv = document.getElementById ("div1");
var date = new Date ();
var ano = date.getlyear (); // ano atual
var mês = date.getMonth (); // mês atual
var dados = date.getDate (); // dia
var horas = date.gethours (); // horas
var minuto = date.getminutes (); // minuto
var Second = date.getSeconds (); // Second
var time = ano+"-"+fnw ((mês+1))+"-"+fnw (dados)+""+fnw (horas)+":"+fnw (minuto)+":"+fnw (segundo);
odiv.innerhtml = time;
}
// Completo quando um campo não é um número de dois dígitos e preenche 0.
função fnw (str) {
var num;
str> 10? num = str: num = "0"+str;
retornar num;
}
</script>
</head>
<Body>
<div id = "div1"> </div>
</body>
</html>