Esta función a menudo se usa y se comparte con usted.
Código de función:
La copia del código es la siguiente:
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth ()+1, // mes
"D+": this.getDate (), // Day
"H+": this.gethours (), // hora
"M+": this.getminutes (), // minuto
"S+": this.getSeconds (), // segundo
"Q+": Math.floor ((this.getMonth ()+3)/3), // trimestre
"S": this.getMilliseConds () // milisegundo
}
if (/(y+)/. test (format)) format = format.replace (regexp. $ 1,
(this.getblyar ()+""). Substr (4 - Regexp. $ 1.Length));
para (var k en o) if (nuevo regexp ("(" + k + ")"). Test (formato)))
format = format.replace (regexp. $ 1,
Regexp. $ 1.length == 1? OK] :
("00"+ o [k]). Substr ((""+ o [k]). Longitud));
formato de retorno;
}
Ejemplo de llamada:
Copie el código de la siguiente manera: <script type = "text/javaScript">
setInterval (function () {
var tiempo = new Date (). Format ("HH: MM: SS");
var fecha = nueva fecha (). Format ("aaa yyy-mm-dd");
document.getElementById ("Time"). InnerHtml = Time;
document.getElementById ("fecha"). innerhtml = date;
}, 1000);
</script>
<div id = "fecha"> </div>
<div id = "tiempo"> </div>