Algorithm - How to catch days in a JS week

Asked

Viewed 983 times

5

I have the following situation: I need to set the days of each week within a month, creating an array of JSON objects.

Each position would correspond to a week, for example if there were 5 weeks I would have an array of 5 positions, where each position would be an object where the day of the week would be the key, and the day of the month the value of this key.

example:

0: {segunda:02/05, terça: 03/05 ...}
1: {...quinta:n/n, sexta:n/n}

I tried to do this using the date object Date but I couldn’t, how could I create this algorithm?

  • Your month would be the current or some set?

  • would be the current month @Marconi

  • 1

    And how Monday should appear if it still belongs to the previous month?

  • 1

    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.

  • 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.

  • as for the project, the same would generate for me objects to set in a date Picker for example?

  • @Marcoshenrique, exactly, I did this project to engine a calendar. jsFiddle: http://jsfiddle.net/La7cwne8/

  • @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?

  • 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

Show 4 more comments

1 answer

2


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));

jsFiddle: https://jsfiddle.net/898thd7e/1

I made this code now, test to see if there’s a hidden bug.

  • Dude, I partially did what you did and with your code I found out why I was so wrong. In addition it is worth pointing out that having these two solutions is even easier for me to decide which one to use for my application, I will use both, thank you and much the feedback.

  • @Marcoshenrique I’m glad I helped.

Browser other questions tagged

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