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>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>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>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>