JavaScript - Problemas con un calendario

 
Vista:
sin imagen de perfil

Problemas con un calendario

Publicado por lk2_89 (2 intervenciones) el 28/10/2015 16:09:23
Buenas tardes!!

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<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>
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.162
Oro
Ha mantenido su posición en JavaScript (en relación al último mes)
Gráfica de JavaScript

Problemas con un calendario

Publicado por xve (2100 intervenciones) el 28/10/2015 17:18:42
Hola, tu problema esta en que la función getDay() devuelve 0 para los domingos!!!

La solución es sencilla... si es 0 le pones el valor 7!!!
1
2
3
var dia=fecha.getDay();
if(dia==0)
	dia=7;

Coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar
sin imagen de perfil

Problemas con un calendario

Publicado por lk2_89 (2 intervenciones) el 28/10/2015 18:06:51
¡¡Muchísimas gracias!! He podido solucionarlo gracias a ti!!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar