• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 464
  • Last Modified:

[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.
0
Wim ten Brink
Asked:
Wim ten Brink
  • 4
  • 3
1 Solution
 
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 BrinkAuthor 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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Wim ten BrinkAuthor 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:
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:
thanks.
0
 
Wim ten BrinkAuthor Commented:
You're welcome. :-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now