[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.
LVL 17
Wim ten BrinkSelf-employed developerAsked:
Who is Participating?
 
thirdConnect With a Mentor Commented:
ok try this,

function SystemDate(Months) {
  dateVar = new Date(2006, 0, 30);
  //dateVar = new Date();  //uncomment to get the current date
 
  toDate = new Date(dateVar.getFullYear(), dateVar.getMonth() + Months + 1, 0);
 
  toDay = toDate.getDate() < dateVar.getDate() ? toDate.getDate() : dateVar.getDate();

  newDate = new Date(toDate.getFullYear(), toDate.getMonth(), toDay);
 
  var year = newDate.getFullYear();
  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;
}

document.write(SystemDate(1));
0
 
thirdCommented:
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;
}
0
 
Wim ten BrinkSelf-employed developerAuthor Commented:
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...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
thirdCommented:
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.
0
 
Wim ten BrinkSelf-employed developerAuthor Commented:
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.
0
 
thirdCommented:
thanks.
0
 
Wim ten BrinkSelf-employed developerAuthor Commented:
You're welcome. :-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.