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

650
Visualizações
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 Respostas
Responde à pergunta

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 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