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

144
Vistas
Obtenga el próximo mes a partir de la fecha actual en html usando javascript

Quiero mostrar la etiqueta p con el próximo mes, por ejemplo, ahora es diciembre, pero la etiqueta p debería mostrarse en enero. Si estamos en enero, la etiqueta p debería mostrarse en febrero. Lo que tengo hasta ahora es esto.

 const month = ["January","February","March","April","May","June","July","August","September","October","November","December"]; const d = new Date(); let name = month[d.getMonth()]; document.getElementById("currentmonth").innerHTML = name;
 <!DOCTYPE html> <html> <body> <p id="currentmonth"></p> <p id="nextmonth"></p> </body> </html>

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

0

Simplemente tome una variable y agregue una al valor de getMonth() y si es mayor que 11, cámbielo a 0.

 const month = ["January","February","March","April","May","June","July","August","September","October","November","December"]; const d = new Date(); let name = month[d.getMonth()]; var x = d.getMonth()+1; if(x>11) x=0 document.getElementById("currentmonth").innerHTML = name; document.getElementById("nextmonth").innerHTML = month[x];
 <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width"> <title>JS Bin</title> </head> <body> <p id="currentmonth"></p> <p id="nextmonth"></p> </body> </html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Debe usar setMonth para establecer el próximo mes en la variable d.

 d.setMonth(Month + 1) 

 const month = ["January","February","March","April","May","June","July","August","September","October","November","December"]; let Month = 11 const d = new Date(); d.setMonth(Month + 1) let name = month[d.getMonth()]; document.getElementById("currentmonth").innerHTML = name;
 <!DOCTYPE html> <html> <body> <p id="currentmonth"></p> <p id="nextmonth"></p> </body> </html>

about 4 years ago · Juan Pablo Isaza Denunciar

0

Si el orden de los meses es según los índices de los meses reales, puede hacerlo de la siguiente manera.

y supongo que olvidaste establecer el valor nextMonth

 const month = ["January","February","March","April","May","June","July","August","September","October","November","December"]; const d = new Date(); let name = month[d.getMonth()]; let next = month[(new Date().getMonth()+1)%12]; document.getElementById("currentmonth").innerHTML = `Current: ${name}`; document.getElementById("nextmonth").innerHTML = `Next: ${next}`;
 <!DOCTYPE html> <html> <body> <p id="currentmonth"></p> <p id="nextmonth"></p> </body> </html>

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