|
MÉTODOS PARA FECHA Y HORA
Métodos que nos van a permitir realizar una serie de operaciones o procedimientos utilizando fechas y horas. Lo primero que tendremos que hacer, construir un objeto Date, para que posteriormente podamos utilizar los métodos de fecha y hora.
SINTAXIS DEL OBJETO
var nombre_objeto=new Date();
|
MÉTODOS DE FECHA Y
HORA
|
|
MÉTODO
|
DESCRIPCIÓN
|
|
objeto.toGMTString();
|
|
|
objeto.getDate();
|
|
|
objeto.getMonth()+1;
|
|
|
objeto.getYear();
|
|
|
objeto.getHours();
|
|
|
objeto.getMinutes();
|
|
|
objeto.getSeconds();
|
|
|
Todas estas funciones tienen su pareja
que nos permite modificar sus valores. Su sintaxis es la misma, solo que
ahora comienzan por set. Por ejemplo setMinutes(minutos) cambiará los
minutos de la hora del sistema.
|
EJEMPLO
1
<html>
<head>
<script>
function fecha()
{
var obj_fecha=new Date();
var completo=obj_fecha.toGMTString();
var hora=obj_fecha.getHours();
var minuto=obj_fecha.getMinutes();
var segundo=obj_fecha.getSeconds();
var dia=obj_fecha.getDate();
var mes=obj_fecha.getMonth()+1;
var anis=obj_fecha.getYear();
alert(hora +":" +minuto +":" +segundo);
alert(dia +"/" +mes +"/" +anis);
alert(completo);
}
</script>
</head>
<body onLoad=fecha();>
</body>
</html>
EJEMPLO 2: Creación de un reloj digital.
<html>
<head>
<script>
setTimeout("reloj()",100);
function reloj()
{
var tiempo=new Date();
var hora=tiempo.getHours();
var minuto=tiempo.getMinutes();
var segundo=tiempo.getSeconds();
var textohora=hora+":"+minuto+":"+segundo;
caja.value=textohora;
setTimeout("reloj()",500);
}
</script>
</head>
<body>
<input type="text" name="caja" size="10">
</body>
<html>
CONTINUAR
>
|