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

302
Vistas
Get year and month formatted in JavaScript

I need to get the year and month in JavaScript in a specific format, but cannot figure out how. I need it like this:

2022-03

I've tried using these functions but I need the 0 in the month:

console.log(new Date().getFullYear())
console.log(new Date().getMonth() + 1)

but the result is 2022 and 3

How can I format dates in javascript?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do something like this, append leading zero to date and slice last two digits

let d = new Date();
date = [
  d.getFullYear(),
  ('0' + (d.getMonth() + 1)).slice(-2)
].join('-');

console.log(date)

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