0

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)); 
Kevin.a
  • 3,482
  • 4
  • 28
  • 65
  • 3
    Making a lookup table to map month number to month name seems pretty easy to me. – Pointy May 06 '20 at 13:44
  • Also you really don't need to construct two Date instances. Make one, then add 4 to the month number as you're already doing, and return that object. – Pointy May 06 '20 at 13:45
  • yes it is an easy solution , but all I am wondering is if there's no built in method that lets me do this. @Pointy – Kevin.a May 06 '20 at 13:45
  • 1
    There isn't anything in the language proper, though something like Moment can do it. If that's all you need, using a library seems like overkill. – Pointy May 06 '20 at 13:47
  • @Pointy Okay thank you for your time and advice on the date instances – Kevin.a May 06 '20 at 13:49
  • 1
    Use an array instead of an object (eg: `lookUpTable = ['Jan', 'Feb', 'Mar', ..]`). The month value starts from `0`, which means that `Jan` should be `0` not `1`. – Titus May 06 '20 at 13:51

3 Answers3

2

A best way with ES 6

var date = new Date();

console.log(date.toLocaleString('default', { month: 'long' }))
Kenny
  • 5,168
  • 3
  • 17
  • 34
1

The only way I know of to do this using a native JavaScript function would be to use one of the functions which formats the Date as a string and parse out the month, but as mentioned in the comments, a lookup table would be much easier. You could do something like this:

const incrementDateByMonths = (months) => {
  if(!months) return; 
  const date = new Date();
   
  const newDate = new Date(date.setMonth(date.getMonth()+months));
  return newDate.toDateString().split(' ')[1];
}

console.log(incrementDateByMonths(1));

Also, your lookup table is off by 1. The months are 0-indexed, so January should be 0, February should be 1, and so on

awarrier99
  • 3,248
  • 1
  • 8
  • 17
1

According to an awesome article: https://css-tricks.com/everything-you-need-to-know-about-date-in-javascript/

const months = [
  'January',
  'February',
  'March',
  'April',
  'May',
  'June',
  'July',
  'August',
  'September',
  'October',
  'November',
  'December'
]

So you can get the month name like:

const monthIndex = new Date(date.setMonth(date.getMonth()+months)).getMonth();
const monthName = months[monthIndex];
console.log(monthName) // September
motou
  • 684
  • 4
  • 9