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

644
Vistas
Luxon - how to get number of nth days between two dates

Let's assume that there are two dates: 2022-01-10 and 2022-08-20.
User can type a day of a month when an action needs to be executed repeatedly once per month between these dates.
So if user types "25" the action should be executed 7 times, because the end date is out of range.

How to get such result using luxon in JavaScript? I need to have number of matching dates OR (that would be even better) a list of these dates.

Another example:

Input:
Day of a month: 5
Start date: 2022-07-08
End date: 2022-09-05

Expected output:
2022-08-05
2022-09-05

Thanks in advance.

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

0

A little hacky but this should get you on a decent path...

const startDate = luxon.DateTime.utc(2022, 1, 5, 0, 0, 0, 0)
const endDate = luxon.DateTime.utc(2022, 6, 5, 0, 0, 0, 0)

const diff = endDate.diff(startDate, ["months"])

// we get a diff of `5` here, let's generate the date map next
console.log(diff.toObject())

const possibleDates = []
const dayOfMonthInput = 2
const targetDateByInput = luxon.DateTime.utc(2022, 1, dayOfMonthInput, 0, 0, 0, 0)

for (
  let i = 0,maybeDate=targetDateByInput;
  i <= diff.months;
  i++, maybeDate = targetDateByInput.plus({ months: i })) {
   // is maybeDate valid after START date?
   if (maybeDate.startOf('day') > startDate.startOf('day'))  {
      // is maybeDate valid BEFORE end date?
     if (maybeDate.startOf('day') < endDate.startOf('day')) {
        // add to collection
        possibleDates.push(maybeDate)
     }
   }
}

// output possible dates
console.log(possibleDates)
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>

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