Calculation for days of months (calendar)

Asked

Viewed 2,018 times

2

I have an array that contains all 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 make for the different days of the months, and for the month of February.

  • I don’t understand what you’re getting at.

  • Distinguish the months with your days, so you can present in a table. In the case of February, or 28 or 29 days.

  • 1

    I still don’t get it, but you want one array with the total days of each month? Something like this? var ultimoDiaMeses= new Array(31,28,31,30,31,30,31,31,30,31,30,31);. I would have to do a leap check to use 28 or 29 days for February. I can’t imagine what those 27 days you talked about.

  • Yes I have a cycle to show every month until the end of the array (months.length). Now what condition to show home days.

1 answer

3


Here is 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>
Insira ano: <input type="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 I used this value to make an internal loop that inserts each day into the days array.

The second part is to show in HTML. In the background is the same thing, 2 loops to iterate every day.

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

  • I am trying to use your function, the days of the months. I want to show in a table every month with your days. http://jsfiddle.net/h4Lc723t/

  • @akm http://jsfiddle.net/h4Lc723t/1/ - lacked only jsFiddle switch to no-wrap on the menu.

  • Right, but the months come all the way to the 31st.

  • @akm I don’t see my code in your jsfiddle.

  • http://jsfiddle.net/h4Lc723t/2/ I added the cycle for the days of the months

Browser other questions tagged

You are not signed in. Login or sign up in order to post.