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

336
Visualizações
How to format date to mm/dd/yyyy in moment / date-fns?

Here I am receiving the last updated data as an API response like this

let data = {
          "year": 2021,
          "monthValue": 11,
          "dayOfMonth": 9
         }

I have to covert this above object into this format MM/DD/YYYY.

Can convert the above data using string interpolation like this ${data.dayOfMonth}/${data.monthValue}/${data.year} But is there any other way we can use libraries to get this work done.

For e.g in the above string interpolation method, can't able to add 0 at the beginning of the dayOfMonth and monthValue when they are given as single digits.

can we achieve this using moment or date-fns?

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

0

You don't need a library just to zero-pad a 1–2 digit number:

function pad2 (n) {
  return String(n).padStart(2, '0');
}

function format (data) {
  return `${pad2(data.monthValue)}/${pad2(data.dayOfMonth)}/${data.year}`;
}


const data = {
  "year": 2021,
  "monthValue": 11,
  "dayOfMonth": 9,
};

const result = format(data);
console.log(result); // 11/09/2021

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