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

177
Visualizações
Javascript Insert Date without leading zeros

I am absolutely new to Javascript. I am trying to get a certain format for a date I need inserted. Essentially, I want it to look like mm/dd/yy but I do not want leading zeros when there is a single digit month or date. Is this possible? I tried the code here but it gives me leading zeros and a four digit year.

Thank you all in advance.

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

0

Try this, it does what you want:

let today = new Date();
const day = String(today.getDate())
const month = String(today.getMonth() + 1); // January is counted as 0
let year = today.getFullYear();

year = `${year}`.split("").splice(2,3).join("")

today = `${day}/${month}/${year}`;
document.write(today);

about 4 years ago · Juan Pablo Isaza Relatório

0

I think you are looking at the top votes answer from the question you are looking at.

In the answer, they use the padstart() to insert 0 for single day.

The padStart() method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length. The padding is applied from the start of the current string.

The simplest solution is to just remove the two padStart from that answer.

var today = new Date();
var dd = String(today.getDate())
var mm = String(today.getMonth() + 1); //January is 0!
var yyyy = today.getFullYear();

today = mm + '/' + dd + '/' + yyyy;
document.write(today);

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