Link to home
Start Free TrialLog in
Avatar of Wim ten Brink
Wim ten BrinkFlag for Netherlands

asked on

[MSXML] Adding months to a date.

Okay, what I need might sound simple but becomes a bit more complicated due to requirements. Since I don't do much javascripting, this is causing me a bit of a problem...

I need to add a number of months to the current date. However, I cannot just add 30 days for every month. So I have something like this:

// Determines the current date plus <Months> months.
function SystemDate(Months) {
  dateVar = new Date();
  var year = dateVar.getYear();
  var month = dateVar.getMonth() + 1 + Months;
  var day = dateVar.getDate();
  if (month == "NaN") {month = dateVar.getMonth() + 1};                   
  while(month > 12) {
    year = year + 1;
    month = month - 12;                     
  };
  if (month < 10) { month = '0' + month; };
  if (day < 10) { day = '0' + day; };
  return year + '-' + month + '-' + day;
}

And yes, it needs to be returned as a string... Unfortunately, if I add one month to 30-01-2006 I get 30-02-2006 and that's NOT a valid date. What I would want to have in this case is the 28-02-2006 instead. (Or 29 if it was a leapyear.)
And I think this code can be a bit more optimized too.

Just one more limiting factor, which also explains why a Delphi developer suddenly has to use JavaScript... :-) This piece of JavaScript is part of an XSLT transformation which my application uses, combined with MSXML, to transform one XML file to another file. Apparantly, this causes some limitations with JavaScript... One limitation: it must always return a valid value which is the current date in case some exception occurs.
Avatar of third
third
Flag of Philippines image

ok try this,

// Determines the current date plus <Months> months.
function SystemDate(Months) {
  dateVar = new Date(2006, 0, 31);
  //dateVar = new Date();  //uncomment to get the current date
  newDate = new Date(dateVar.getYear(), (dateVar.getMonth()*1) + Months, dateVar.getDate());

  var year = newDate.getYear();
  var month = newDate.getMonth() + 1;
  var day = newDate.getDate();
 
  if (month < 10) { month = '0' + month; };
  if (day < 10) { day = '0' + day; };

  return year + '-' + month + '-' + day;
}
Avatar of Wim ten Brink

ASKER

I don't think that will work if the month overflows into the next year. Btw, the function I have will work in most cases, but not in all. The main problem is the different number of days per month, and of course the problem of leapyears...
Hi,

After re-reading your question, I became a bit confused by your example. if the date is

30-01-2006, adding 1 month results to 28-02-2006

or you mean if the date is

31-01-2006, adding 1 month results to 28-02-2006 (or 29 if leap year)


what would be the result for 28-01-2006 and 01-01-2006?

Getting the number of days in a month is easy. Will post my solution after the above is clarified.
Okay, adding one month to (DD-MM-YYYY):
01-01-2006 -> 01-02-2006
28-01-2006 -> 28-02-2006
29-01-2006 -> 28-02-2006
29-01-2004 -> 29-02-2004 (!)
31-01-2006 -> 28-02-2006
Etc.

Basically, the value needs to just add to the month, keep in mind all leapyears and of course make sure no date will overflow. Yeah, the code isn't that complex but I'm looking for the most-optimized one. And JavaScript isn't really my favorite language but in this case I have no other option since the application I am working on must transform some XML data.
ASKER CERTIFIED SOLUTION
Avatar of third
third
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks.
You're welcome. :-)