-1

Here is my Code. My question is how can I show the next month when my current month is Dec.

var monthNames = ['January','February','March','April','May','June','July','August','September','October','November','December'];

 console.log(monthNames[new Date().getMonth()].toString().substr(0, 3) + '-' + new Date().getFullYear().toString().substr(-2));

console.log(monthNames[new Date().getMonth()+1].toString().substr(0, 3) + '-' + new Date().getFullYear().toString().substr(-2));

 console.log(monthNames[new Date().getMonth()+2].toString().substr(0, 3) + '-' + new Date().getFullYear().toString().substr(-2));

  console.log(monthNames[new Date().getMonth()+3].toString().substr(0, 3) + '-' + new Date().getFullYear().toString().substr(-2));

 console.log('Suppose if the current month is Dec-19 then what should I do? It should return next line as Jan-20 but it is not doing so');

This is my JSFIDDLE Fiddle

If my month is Dec 17 or Dec 18 how can I show the next month ? I need a vanilla JS solution, no jQuery, no lodash, no 3rd party libraries.

tommy123
  • 587
  • 1
  • 10
  • 28
  • add one to month in the date object... date object does the rest for you – Jaromanda X Jan 10 '19 at 07:31
  • Possible duplicate of https://stackoverflow.com/questions/2706125/javascript-function-to-add-x-months-to-a-date – misorude Jan 10 '19 at 07:33
  • have a look to the [reminder operator `%`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Remainder_()). just add an offset and take the result with the remainder operator with the length of the array. – Nina Scholz Jan 10 '19 at 07:34
  • @Nina Scholz can you kindly show me ? I can't understand. If month is Dec 18 next month will be Jan 19. If current Month is Nov 18 , month + 2 should give me Jan 19 – tommy123 Jan 10 '19 at 07:47
  • why not the same day? – Nina Scholz Jan 10 '19 at 07:49
  • @JaromandaX can you show me ? – tommy123 Jan 10 '19 at 07:49
  • `dateobject.setMonth(dateobject.getMonth() + 1)` - of course ... you're not doing that currently so that won't help much – Jaromanda X Jan 10 '19 at 07:51
  • @Nina Scholz I can't get your point. I need to show month, next month, 2nd next month along with year. I don't need to show date and day – tommy123 Jan 10 '19 at 07:53
  • @JaromandaX can you show me in my small fiddle ? – tommy123 Jan 10 '19 at 07:54
  • @JaromandaX how can I get year if my present month is Dec and I need to show month + 2 along with year – tommy123 Jan 10 '19 at 08:05

2 Answers2

1

Use the date object, and it's built in functions. Take a vow now to never create an array of month names! setMonth() happily takes values greater than 12, or negative values, so just do what you need...

var myDate = new Date('2019-12-19')
myDate.setMonth( myDate.getMonth()+1 )
var formatted = myDate.toLocaleDateString({},{month:'long',year:'2-digit'})
console.log(formatted)
bbsimonbb
  • 27,056
  • 15
  • 80
  • 110
1

I think this one will be enough

monthNames[(new Date().getMonth() % 12)]
duc mai
  • 1,412
  • 2
  • 10
  • 17