Calculate date with Javascript or otherwise, even in excel?

Asked

Viewed 85 times

2

How to arrive at an end date from a start date multiplying by N weeks, not taking into account the last week of the year. I can calculate the dates, but I can’t cash the last week.

  • 2

    It’s a little wide your question, right? Choose a language. You can do it in different ways in each of them.

2 answers

2

Suggestion using Javascript:

function futuro(dataInicial, semanas) {
    var data = new Date(dataInicial); // para evitar mudar o objeto original
    var ano = data.getFullYear(); // saber o ano em que está
    var intervalo = semanas * 7 * 86400000; // calcular os milisegundos de diferença porque o JavaScript trabalha com milisegundos
    data.setTime(data.getTime() + intervalo); // aplicar o intervalo à data
    if (data.getFullYear() != ano) data.setTime(data.getTime() - 7 * 86400000); // tirar uma semana, caso o ano mude
    return data;
}

var hoje = new Date();
var dezembro = new Date(2016, 11, 1);

console.log(futuro(hoje, 2)); // dá Mon Jun 27 2016 06:55:25 GMT+0100 (W. Europe Standard Time)
console.log(futuro(dezembro, 6)); // dá Thu Jan 05 2017 00:00:00 GMT+0100 (W. Europe Standard Time)

jsFiddle: https://jsfiddle.net/j14w6kkv/1/

0

If this is what you need, I did in Excel, but since you were not specific, I considered only the days belonging to the last week of the year, and not seven days until 31 December inclusive.

inserir a descrição da imagem aqui

The final two cells should be formatted for "date".

In the case of the final formula, calculate the difference between the dates of the last week of the year and add one, to get the amount of days correctly to be discounted.

If you consider the last week different from what I did, just adjust the formula.

  • Hi Claudio, served for you friend? If yes, mark the answer, please.

Browser other questions tagged

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