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

260
Vistas
How to manipulate date using Javascript

How can I achieve this sequence of date output if the user input is 04-29-2022 and the output is like this

What I want:
2022-05-14
2022-05-29
2022-06-14
2022-06-29
2022-07-14
2022-07-29
2022-08-14
2022-08-29

my code output
2022-05-13
2022-05-28
2022-06-12
2022-06-27
2022-07-12
2022-07-27
2022-08-11
2022-08-26

var dateRelease = new Date("04-29-2022")

const terms = 8
for (let i = 0; i < terms; i++) {
  console.log(new Date(dateRelease.setDate(dateRelease.getDate() + 15)).toISOString().slice(0, 10))

}

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Here is a function that takes the day of the month in the given date and determines which other date of the month would be needed in the output (either 15 more or 15 less). Then it generates dates alternating between those two date-of-the-month, and when it is the lesser of those two, incrementing the month. In case a date is invalid and automatically overflows into the next month, it is corrected to the last day of the intended month.

To format the date in the output, it is better to not use toISODateString as that interprets the date as a UTC Date, while new Date("2022-04-29") interprets the given string as a date in the local time zone. This can lead to surprises in some time zones. So I would suggest using toLocaleDateString with a locale that produces your desired format.

Here is the code:

function createSchedule(date, count) {
    date = new Date(date);
    let day = date.getDate();
    let k = +(day > 15);
    let days = k ? [day - 15, day] : [day, day + 15];
    let result = [];
    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("2022-04-29");
var result = createSchedule(dateRelease, 25);
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

var dateRelease = new Date("04-29-2022")
const terms = 8
for (let i = 0; i < terms; i++) {
  let date = new Date(dateRelease.setDate(dateRelease.getDate() + 15)).toLocaleDateString('en-US');
  let format = date.split('/').map(d => d.padStart(2 ,'0')).join('-')
  console.log(format);
}

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