Solved

[MSXML] Adding months to a date.

Posted on 2006-10-20
7
455 Views
Last Modified: 2012-05-05
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
Comment
Question by:Wim ten Brink
  • 4
  • 3
7 Comments
 
LVL 30

Expert Comment

by:third
ID: 17782578
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
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17784826
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
 
LVL 30

Expert Comment

by:third
ID: 17817884
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17818022
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
 
LVL 30

Accepted Solution

by:
third earned 500 total points
ID: 17824038
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
 
LVL 30

Expert Comment

by:third
ID: 17838715
thanks.
0
 
LVL 17

Author Comment

by:Wim ten Brink
ID: 17840409
You're welcome. :-)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now