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

231
Visualizações
How to get month index from month name using date-fns library

I need to get the month index number from the month name using the date-fns library. Earlier I was using moment.js, it was straightforward with moment.js

  moment().month('March').format('M'); 
// Month name will be variable and I don't want to use static dictionary to get index number

How to achieve the same using the date-fns library? I thought to use format(date, 'M'); but it needs complete date and only month name is available.

Please suggest. Thanks!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I think this is what you need

If you are using date-fns library

function getMonthCount(month){
    const result = getMonth(new Date(`${month} 1`))+1;
}

If you don't want to use any library

function getMonthCount(month){
    const sometime= new Date(`${month} 1`);
    console.log(sometime.getMonth()+1);
}

NOTE: this will return month in range [1,12]

about 4 years ago · Juan Pablo Isaza Relatório

0

date-fns comes with handy functions out of the box

import {getMonth} from 'date-fns'

const date = new Date()

const month = getMonth(date) + 1

getMonth returns a number, you add +1 because it starts with 0. The result you'll get is the number of the month. If let's say the month is March, you'll get 3

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