Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Problemas en calendario

Estas en el tema de Problemas en calendario en el foro de Javascript en Foros del Web. Buenos días!! Estoy haciendo un calendario en javascript pero tengo un problema que no soy capaz de ver... Cuando consulto algún mes en el que ...
  #1 (permalink)  
Antiguo 27/10/2015, 02:25
 
Fecha de Ingreso: agosto-2007
Mensajes: 71
Antigüedad: 16 años, 8 meses
Puntos: 1
Problemas en calendario

Buenos días!!

Estoy haciendo un calendario en javascript pero tengo un problema que no soy capaz de ver... Cuando consulto algún mes en el que el día uno empieza en domingo (véase marzo o noviembre de este año), me lo pinta como lunes... Estoy harto de darle vueltas al código y no encuentro el error.

¿Alguna idea? Muchas gracias.

Código:
<html>
<head><title>Superejercicio - Calendario</title></head>					
<body>

<center>		
<b>CALENDARIO</b>

<form name="calendario" action="">
<p>AÑO	<input type="text" name="año" id="año"  /></p>
<p>MES	<select name="mes" id="mes" size="1" /></p>
<option value="12">mes</option>
<option value="0">Enero</option>
<option value="1">Febrero</option>
<option value="2">Marzo</option>
<option value="3">Abril</option>	
<option value="4">Mayo</option>
<option value="5">Junio</option>
<option value="6">Julio</option>
<option value="7">Agosto</option>
<option value="8">Septiembre</option>
<option value="9">Octubre</option>
<option value="10">Noviembre</option>
<option value="11">Diciembre</option>
</select>		
<input type="submit" value="Calendario" />
</form>

</center>

<script>
var j; var k;
var año=location.search.split("&")[0].split("=")[1];
var mes=location.search.split("&")[1].split("=")[1];
var fecha=new Date();

fecha.setFullYear(año);
fecha.setMonth(mes);
fecha.setDate(+1);
var dia=fecha.getDay();

document.calendario.año.value=año;
document.calendario.mes.value=mes;
document.write("<br><br><br>");
document.write("<center><table>");
document.write("<tr  bgcolor=red >");
document.write("<th><b>Lunes</b><\/th>");
document.write("<th><b>Martes</b><\/th>");
document.write("<th><b>Miercoles</b><\/th>");
document.write("<th><b>Jueves</b><\/th>");
document.write("<th><b>Viernes</b><\/th>");
document.write("<th><b>Sabado</b><\/th>");
document.write("<th><b>Domingo</b><\/th>");
document.write("<\/tr>");
document.write("<tr>");

for(i=1;i<=7;i++)
{
if(i<dia)
	{
	document.write("<td>  <\/td>");
	}
else 
	{
	document.write("<td><script>document.write(fecha.getDate())<\/script><\/td>");
	fecha.setDate(fecha.getDate()+1);
	}
}
document.write("<\/tr>");

for(k=1;k<=5;k++)
{
document.write("<tr>");
for(j=1;j<=7;j++)
{
	if(mes!=fecha.getMonth())
	{
		document.write("<td>  <\/td>");
	}
	else
	{
		document.write("<td><script>document.write(fecha.getDate())<\/script><\/td>");
		fecha.setDate(fecha.getDate()+1);
	}
}
document.write("<\/tr>");
if(mes!=fecha.getMonth())
{
break;
}}

document.write("<\/table><\/center>");

</script>
</body>
</html>
  #2 (permalink)  
Antiguo 27/10/2015, 12:57
Avatar de IsaBelM
Colaborador
 
Fecha de Ingreso: junio-2008
Mensajes: 5.032
Antigüedad: 15 años, 10 meses
Puntos: 1012
Respuesta: Problemas en calendario

prueba con este
Cita:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
.c_calendario_general {
width: 400px;
height: auto;
border: 1px solid rgb(195, 192, 192);
z-index: 1;
}


.calendario{
width: 100%;
height: auto;
border-collapse: collapse;
}


.diasemana > th {
border-bottom: 1px solid rgb(195, 192, 192);
}


tr.controles > th {
cursor: default;
}


.Ccasilladia {
width: 38px;
height: 19px;
cursor: default;
text-align: center;
}
</style>
<script type="text/javascript">
var calendario = {

meses : ['Enero', 'Febrero', 'Marzo', 'Abril', 'Mayo', 'Junio', 'Julio', 'Agosto', 'Septiembre', 'Octubre', 'Noviembre', 'Diciembre'],
diasemana : ['Lun', 'Mar', 'Mié', 'Jue', 'Vie', 'Sáb', 'Dom'],
fechaHoy : null,
cTabla : null,
tBody : null,
formatoFecha : 'dd/mm/yyyy',


cabCalendario : function() {

var fecha = new Date();
calendario.fechaHoy = fecha;

var objcalendario = document.querySelectorAll('.c_calendario_general') ;

calendario.cTabla = document.createElement('table');
calendario.cTabla.setAttribute('class', 'calendario');
var tHead = document.createElement('thead');
var cTHead = calendario.cTabla.appendChild(tHead);
var trH1 = document.createElement('tr');
trH1.setAttribute('class', 'controles');
var cTrHead = cTHead.appendChild(trH1);
var thH1 = document.createElement('th');
thH1.setAttribute('class', 'atrasy');
thH1.setAttribute('colspan', '1');
thH1.setAttribute('title', 'Retroceder un año');
thH1.textContent = '<<';
trH1.appendChild(thH1);
var thH2 = document.createElement('th');
thH2.setAttribute('class', 'atrasm');
thH2.setAttribute('colspan', '1');
thH2.setAttribute('title', 'Retroceder un mes');
thH2.textContent = '<';
trH1.appendChild(thH2);
var thH3 = document.createElement('th');
thH3.setAttribute('class', 'myyyy');
thH3.setAttribute('colspan', '3');
thH3.textContent = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();
trH1.appendChild(thH3);
var thH4 = document.createElement('th');
thH4.setAttribute('class', 'adelantem');
thH4.setAttribute('colspan', '1');
thH4.setAttribute('title', 'Avanzar un mes');
thH4.textContent = '>';
trH1.appendChild(thH4);
var thH5 = document.createElement('th');
thH5.setAttribute('class', 'adelantey');
thH5.setAttribute('colspan', '1');
thH5.setAttribute('title', 'Avanzar un año');
thH5.textContent = '>>';
trH1.appendChild(thH5);
var trH2 = document.createElement('tr');
trH2.setAttribute('class', 'diasemana');
var cTrHead = cTHead.appendChild(trH2);

for (var i = 0; i < 7; i++) {

var thH = document.createElement('th');
thH.textContent = calendario.diasemana[i];
trH2.appendChild(thH);
}


objcalendario[0].appendChild(calendario.cTabla);

calendario.cuerpoCalendario();

document.querySelector('th.atrasy').addEventListen er('click', function(){calendario.retrocede(false)}, false);
document.querySelector('th.atrasm').addEventListen er('click', function(){calendario.retrocede(true)}, false);
document.querySelector('th.adelantey').addEventLis tener('click', function(){calendario.avanza(false)}, false);
document.querySelector('th.adelantem').addEventLis tener('click', function(){calendario.avanza(true)}, false);
},




cuerpoCalendario : function() {

calendario.cBody = document.createElement('tbody');

//variable fechaaux a modo de fecha auxiliar, la situo el dia 1 del mes y año actual
var fechaaux = new Date(calendario.fechaHoy.getFullYear(), calendario.fechaHoy.getMonth(), 1);
//saco el dia de la semana que es el 1 del mes actual
diaactual = fechaaux.getDay();
//y voy retrasando la fecha de dia en dia hasta que llego a un lunes, una vez llegue a este lunes, sera la primera fecha que se pinte en el calendario
while (diaactual != 1) {

fechaaux.setDate(fechaaux.getDate() - 1);
diaactual = fechaaux.getDay();
}

// creamos los dias en el calendario, pintamos el dia que contiene fechaaux y la pasamos al dia siguiente
for (var i = 0; i < 6; i++) {

var trB = document.createElement('tr');
var cTrBody = calendario.cBody.appendChild(trB);

for (var j = 0; j < 7; j++) {

if (calendario.fechaHoy.getMonth() == fechaaux.getMonth()) {

var tdB = document.createElement('td');
tdB.setAttribute('class', 'Ccasilladia');

if (calendario.fechaHoy.getDate() == fechaaux.getDate()) {

configcolor = 'color: rgb(255, 255, 255); background-color: rgb(116, 116, 117);'; //CASILLA DE HOY
tdB.setAttribute('style', configcolor);

} else if ((fechaaux.getDay() == 6) || (fechaaux.getDay() == 0)) { //si el dia actual es sabado o domingo

configcolor = 'color: rgb(255, 0, 0);';
tdB.setAttribute('style', configcolor);

} else {

configcolor = '';
}

tdB.textContent = fechaaux.getDate();
var cTdBody = cTrBody.appendChild(tdB);
tdB.addEventListener('click', function() {calendario.mostrarFecha(this.textContent)});

} else {

var tdB = document.createElement('td');
var cTdBody = cTrBody.appendChild(tdB);

}

fechaaux.setDate(fechaaux.getDate() + 1);
}

}

calendario.cTabla.appendChild(calendario.cBody);

},



avanza: function(aniomes) {

if (!aniomes) {

calendario.fechaHoy.setFullYear(calendario.fechaHo y.getFullYear() + 1);


} else {

calendario.fechaHoy.setMonth(calendario.fechaHoy.g etMonth() + 1);

}

document.querySelector('.myyyy').innerHTML = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();

calendario.cTabla.removeChild(calendario.cBody);

calendario.cuerpoCalendario();
},



retrocede: function(aniomes) {

if (!aniomes) {

calendario.fechaHoy.setFullYear(calendario.fechaHo y.getFullYear() - 1);

} else {

calendario.fechaHoy.setMonth(calendario.fechaHoy.g etMonth() - 1);

}

document.querySelector('.myyyy').innerHTML = calendario.meses[calendario.fechaHoy.getMonth()] + ', ' + calendario.fechaHoy.getFullYear();

calendario.cTabla.removeChild(calendario.cBody);

calendario.cuerpoCalendario();
},



mostrarFecha : function(dia) {

var fecha = [calendario.fechaHoy.getFullYear(), calendario.fechaHoy.getMonth() + 1, dia];

document.getElementById('fentrada').textContent = calendario.formatFecha(fecha, calendario.formatoFecha);
},



formatFecha : function(arr, darformato) {

darformato = darformato.replace(/(d+)/g, function(v) {

return ((arr[2].length < 2 && v.length > 1 ? '0' : '') + arr[2]);
});


darformato = darformato.replace(/(m+)/g, function(v) {

return ((arr[1].toString().length < 2 && v.length > 1 ? '0' : '') + arr[1]);
});

return darformato.replace(/(y+)/g, function(v) {

return arr[0].toString().slice(-v.length);
});

}


}


window.addEventListener('load', calendario.cabCalendario, false);
</script>
</head>
<body>

<div class="c_calendario_general"></div>
<div id="fentrada"></div>

</body>
</html>
__________________
if(ViolenciaDeGénero) {alert('MUJER ASESINADA');}
  #3 (permalink)  
Antiguo 27/10/2015, 18:07
 
Fecha de Ingreso: agosto-2007
Mensajes: 71
Antigüedad: 16 años, 8 meses
Puntos: 1
Respuesta: Problemas en calendario

Muchas gracias IsaBelM, pero me gustaría intentar arreglar mi código..

EDITO: ya he podido solucionarlo

Última edición por lk2_89; 28/10/2015 a las 11:05

Etiquetas: calendario, html, input, select
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 16:58.