Calculation for days of months (calendar)

2

I have an array that contains all the months of the year. I want to create a calendar.

var meses= new Array("Janeiro","Fevereiro","Marco","Abril","Maio","Junho","Julho","Agosto","Septembro","Outubro","Novembro","Dezembro");

With this array, how to do for the different days of the months, and for the month of February.

    
asked by anonymous 20.10.2014 / 22:34

1 answer

3

Here's a suggestion:

var meses = new Array("Janeiro", "Fevereiro", "Marco", "Abril", "Maio", "Junho", "Julho", "Agosto", "Septembro", "Outubro", "Novembro", "Dezembro");

function diasDoAno() {
    var ano = $('#ano').val();
    var dias = [];
    for (var x = 0; x < 12; x++) {
        var diasNoMes = new Date(ano, x + 1, 0).getDate();
        dias[x] = [];
        for (var i = 1; i <= diasNoMes; i++) {
            dias[x].push(i);
        }
    }
    var resultado = $('#resultado');
    dias.forEach(function (mes, i) {
        var div = $('<div />').addClass('mes');
        div.append(meses[i]);
        var subDiv = $('<div />').addClass('dias');
        mes.forEach(function (dia) {
            subDiv.append(dia);
        });
        div.append(subDiv);
        resultado.append(div);
    });
}


$('button').on('click', diasDoAno);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>Insiraano:<inputtype="text" id="ano"/>
<button>Mostrar dias</button>
<div id="resultado"></div>

This function has two parts. The first creates an array with 12 months, each element of the array with the days of that month.

To know how many days each month have I used var diasNoMes = new Date(ano, x + 1, 0).getDate(); and then used this value to make an internal loop that inserts each day into the array of days.

The second part is to display in HTML. In the background it's the same thing, 2 loops to iterate all the days.

I leave the part of using the array that the first part creates for you.

    
20.10.2014 / 23:35