Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

234
Visualizações
How to manipulate dates for Tri-monthly payment dates using javascript

How to can I make this sequence of date with tri-monthly. for eg. the input is `"2022-03-14" the input is dynamic it depends on the user input... I'm trying add + 10 days but isn't working

The output I want

 [
      "2022-03-24",
      "2022-04-04",
      "2022-04-14",
      "2022-04-24",
      "2022-05-04",
      "2022-05-14",
      "2022-05-24",
      "2022-06-04",
      "2022-06-14",
      "2022-06-24",
    ]

My code output which is worng

[
  "2022-03-24",
  "2022-04-14",
  "2022-04-24",
  "2022-05-14",
  "2022-05-24",
  "2022-06-14",
  "2022-06-24",
  "2022-07-14",
  "2022-07-24",
  
]

function createSchedule(date, count){
date = new Date(date); 
let day = date.getDate();// Get day in given date 
let k = 0;
let days = k? [day - 10, day , day + 10] : [day, day + 10, day- 10];
let result = [];

    if(day > 10){
        k = +0
    }else{
        if(day > 20 ){
        k = +1
    }else{
        k= +2
        }
    }


for(let i = 0; i < count; i++){
    k= 1-k; 
    date.setDate(days[k]);
    // When date overflows into next month, take last day of month
    if (date.getDate() !== days[k]) date.setDate(0);        
    if (!k) date.setMonth(date.getMonth() + 1);
    result.push(date.toLocaleDateString("en-SE"));
}
    return  result

}
            
var dateRelease = new Date("03-14-2022");
var result = createSchedule(dateRelease, 9);
console.log(result)

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

A few issues in your attempt:

  • After let k = 0, the conditional operator on k? will always evaluate the first expression after ?, which is [day - 10, day , day + 10].
  • That array could have dates that are greater than 31 (day + 10)
  • That other array [day, day + 10, day- 10] is not sorted, but should be.
  • The constants +0 and +1 and +2 are OK, but it looks odd that you use the unary plus here. It could just be 0, 1 and 2.
  • The assignment k = 1 - k assumes you only have two entries in your days array, but you have three, so use modular arithmetic: k = (k + 1) % 3

Here is a correction:

function createSchedule(date, count) {
    date = new Date(date);
    let day = date.getDate();
    let firstDay = 1 + (day - 1) % 10;
    let days = [firstDay, firstDay + 10, firstDay + 20];
    let k = days.indexOf(day);
    let result = [];
    for (let i = 0; i < count; i++) {
        k = (k + 1) % 3;
        date.setDate(days[k]);
        // When date overflows into next month, take last day of month
        if (date.getDate() !== days[k]) date.setDate(0);         
        if (!k) date.setMonth(date.getMonth() + 1);
        result.push(date.toLocaleDateString("en-SE"));
    }
    return result;
}

var dateRelease = new Date("2022-03-14");
var result = createSchedule(dateRelease, 25);
console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda