Created
          May 16, 2016 09:51 
        
      - 
      
- 
        Save heroqu/87416422b3a0ad7e138c239cfbe01730 to your computer and use it in GitHub Desktop. 
    Add / subtract number of months from given date (original date in param stays immutable).
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | function addMonths(date, months) { | |
| var d = new Date(date.getTime()); | |
| var m = d.getMonth(); | |
| var y = d.getFullYear(); | |
| var ym = y * 12 + m; | |
| ym = ym + months; | |
| m = ym % 12; | |
| y = (ym - m) / 12; | |
| d.setYear(y); | |
| d.setMonth(m); | |
| return d; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment