I wrote this little code that gets a date in the future based on your input in months :
const incrementDateByMonths = (months) => {
if(!months) return;
const date = new Date();
const newDate = new Date(date.setMonth(date.getMonth()+months));
return newDate;
}
console.log(incrementDateByMonths(4)); //May + 4 months === september
The problem is that this returns a whole date and I just want the month. In this case I want my return statement to return 'September'. I could do getMonth with javascript and then have a lookup table as an object with key value pairs. But I am wondering if there's an easier way.
This is how I imagine my example to work :
const incrementDateByMonths = (months) => {
if(!months) return;
const date = new Date();
const lookUpTable =
{
1 : 'Jan',
2 : 'Feb',
3 : 'Mar',
4 : 'Apr',
5 : 'may',
6 : ' jun'
}
const newDate = new Date(date.setMonth(date.getMonth()+months)).getMonth();
return lookUpTable[newDate];
}
console.log(incrementDateByMonths(1));