As I suggested in the comments take a look at this project js-Calendar (demo), because I think it does a lot of what you need. It’s a date generator with weekdays, etc.
But to answer the specific question, you can do it this way:
var diasSemana = ['Segunda', 'Terça', 'Quarta', 'Quinta', 'Sexta', 'Sabado', 'Domingo'];
function diaSemana(nr) {
return diasSemana[nr % 7]; // dar nomes ao numero do dia de semana
}
function diasMes(ano, mes) {
var semanas = []; // array a preencher
var semana = {};
var diaUm = new Date(ano, mes - 1); // dia um do mês
var inicioSemana = diaUm.getDay() - 1; // numero do dia da semana (começando na segunda)
var ultimoDia = new Date(ano, mes, -1).getDate(); // quantidade de dias no mês
for (var i = 1; i <= ultimoDia; i++) {
var dia = diaSemana(inicioSemana++); // dar nome ao dia da semana
semana[dia] = [i, mes].join('/'); // dia do mês / mês
if (i % 7 == 0) { // caso mude a semana
semanas.push(semana);
semana = {}
} else {
if (i == ultimoDia) semanas.push(semana); // juntar os ultimos dias
}
}
return semanas;
}
var janeiro = diasMes(2016, 1);
alert(JSON.stringify(janeiro, null, 4));
I made this code now, test to see if there’s a hidden bug.
Your month would be the current or some set?
– Marconi
would be the current month @Marconi
– Marcos Henrique
And how Monday should appear if it still belongs to the previous month?
– Sergio
Apart from your question here, but related you can take a look to this project on Github, it generates something similar to what you’re looking for.
– Sergio
i ended up mistaking the example and changing the month, but if the month did not have a second should not be set in this application where it encompasses only the current month.
– Marcos Henrique
as for the project, the same would generate for me objects to set in a date Picker for example?
– Marcos Henrique
@Marcoshenrique, exactly, I did this project to engine a calendar. jsFiddle: http://jsfiddle.net/La7cwne8/
– Sergio
@Marcoshenrique in relation to the question: the array starts with the 1st of each month or the Monday of the first week of the month?
– Sergio
then in relation to the array, it should be set from the first day of the month regardless of the day of the week it started
– Marcos Henrique