Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!