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

145
Visualizações
Get next month from current date in html using javascript

I want to display a p tag with the next month, for example now is December But the p tag should be display January, If we in Jan the p tag should be display Febrary. What I have until now is this.

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 Respostas
Responde à pergunta

0

Simply take a variable and add one to the getMonth() value and if it is greater than 11 then change it to 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 Relatório

0

You have to use setMonth to set the next month on the d variable.

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 Relatório

0

If the order of the months is according to the real month indexes, you can do it as follows.

and i guess you forgot set value 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 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