0

Hi everyone I use Date js library and have a problem with adding additional months.

Here my simple code:

var durationMonth = $('#periodId').find(':selected').data('id'); // value => 3 m
var startDate = $('#comencingDate').val(); // value -> 2015.12.14
$('#expiringDate').val(Date.parse(startDate + ' + ' + durationMonth).toString("yyyy-MM-dd")); 
// This return 2016-03-03, but have to return 2016-03-13

Here little demo http://jsfiddle.net/d9rttxta/1/

The problem is only with months, with days and years work ok. If you have any suggestion I will be very glad to hear. Thanks in advance.

Expected Result: 2016-03-13

Actual Result: 2016-03-03

ragerory
  • 1,360
  • 1
  • 8
  • 27
diank
  • 628
  • 2
  • 11
  • 19
  • see http://stackoverflow.com/q/5645058/1846918 – axel.michel Dec 14 '15 at 20:32
  • yeah, just re-read the question. oops... but in any case, adding months is never an exact thing anyways, since months are variable length. is a month 28 days? 29? 30? 31? what's "jan 31 + 1 month" feb 28? March 3? what's feb 28 + 1month? march 28th? or march 31, because you're going last-day-of-month to last-day-of-next month? – Marc B Dec 14 '15 at 20:39
  • @MarcB yup. Need to use a library that actually has this stuff cooked into it, or make your own. Like someone else mentioned, this is a very old library that doesn't appear to be actively worked on. Or add days. Too much confusion. – ragerory Dec 14 '15 at 20:41
  • It's old as well, but my library does add months: http://depressedpress.com/javascript-extensions/dp_dateextensions/ To Marc B's point: date math is _weird_. I rewrote the entire library about three years ago because I went down a very twisty rabbit hole on that front. Here's a blog post about the thought process: http://depressedpress.com/2012/03/22/new-version-of-dp_dateextensions/ When you consider differences are you talking any ordinal change, conventions or absolute, whole values? I've burned a significant number of brain cells considering this. Weep for them, my friends! ;^) – Jim Davis Dec 14 '15 at 21:29

1 Answers1

2

Adding the month to a date would reset the date-month to the 1st day of the month. So you need to extract the day part out of the value and then append it with the month value.

$('.change').on('click', function() {
            var durationMonth = $('#periodId').find(':selected').data('id');      
            var startDate = $('#comencingDate').val();
            $('#expiringDate').val(Date.parse(Date.parse(startDate).toString("dd") + ' + ' + durationMonth).toString("yyyy-MM-dd"));
});

Working example : http://jsfiddle.net/d9rttxta/3/

UPDATE : In order to add a month to a date which is in future year, here's what you can try :

$('.change').on('click', function() {
            var durationMonth = $('#periodId').find(':selected').data('id');      
            var startDate = $('#comencingDate').val();
      var monthDiff = Date.parse(startDate).getMonth() - new Date().getMonth()
       + (12 * (Date.parse(startDate).getFullYear() - new Date().getFullYear())); 

      var calcDate = Date.parse(Date.parse(startDate).toString("dd") + ' + ' + durationMonth).toString("yyyy-MM-dd");  

      if(monthDiff > 0)
      {
         var n_calcDate = new Date(calcDate);
         n_calcDate.setMonth(n_calcDate.getMonth() + monthDiff);
         n_calcDate.setDate(n_calcDate.getDate() + 1);
         calcDate = n_calcDate.toString("yyyy-MM-dd");
      }

            $('#expiringDate').val(calcDate);
});

Working example : http://jsfiddle.net/d9rttxta/5/

DinoMyte
  • 8,737
  • 1
  • 19
  • 26
  • Nice. Tested it with adding 2 months to 2015-12-31 and I got what I would think it should give me. – ragerory Dec 14 '15 at 21:49
  • Hi DinoMyte, well done, but now the problem is that if I choese "From: 2016-03-14" and then choese "Period 2 months" the result is "2016-02-14" but have to be "2016-05-14". How to fix this problem? Thanks. – diank Dec 15 '15 at 07:24
  • when adding days and months using mnemonic, the javascript takes the current year as default. In order to get it work for any future year, follow the UPDATE section in the solution. Hope that helps. – DinoMyte Dec 16 '15 at 00:07
  • Thanks @DinoMyte, can you show me some demo in jsfiddle? Thanks again. – diank Dec 16 '15 at 08:49