Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda