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

251
Visualizações
Day.js - Get array of remaining months in year

I want to get an array of the index of remaining months (0-index), not including current month, in a countdown, this year (or any date for the sake of it). I'm using lodash and dayjs, but I feel my code is a bit hard to understand.

Is there a more "dayjs" way to get what I want? I have not found more help in the doc's library or other threads with a similar problem here.

// All months - Current year's remaining months (0-index), we map in reverse until reaching 0
const yearRemainingMonths = map(range(11 - dayjs().month()), n => 11 - n)
// []

// Let's pretend we are in June, so we'd get
// [11, 10, 9, 8, 7]
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There's not much a cleaner way. In the end, you have to loop once or to store the predefined array.

Example 1 - with cached data


import dayjs from 'dayjs'

const months = [0,1,2,3,4,5,6,7,8,9,10,11]
function remainingMonths(month) {
  return [...months].splice(month+1).reverse()
}

console.log(remainingMonths(dayjs().month())) // []
console.log(remainingMonths(5))  // june => [ 11,10,9,8,7,6]
console.log(remainingMonths(0))  // [11,10,9,8,7,6,5,4,3,2,1]

Example 2 - with for loop

function remainingMonths(month) {
  const remaining = []
  for(let i = 11; i > month; i--) {
    remaining.push(i)
  }
  return remaining
}

console.log(remainingMonths(11)) // []
console.log(remainingMonths(5))  // june => [ 11,10,9,8,7,6]
console.log(remainingMonths(0))  // [11,10,9,8,7,6,5,4,3,2,1]

Probably the for loop is a bit cleaner.

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