Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda