These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Calculating dates : add 1 month

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
Answered

Hi,

I have a form where end-user enter a date, and I must populate 10 fields that repeat the starting date adding one month in each 10 fields :
01/24/2010
02/24/2010
03/24/2010
and so on…

After searching the forum I found some useful tutorials made by Thomp and gkaiseril, and now I can add 30 days, my script works fine.
My script is :

event.value = "";
// Get the string date from the current date field
var strDate = this.getField("prelev.IDservices_form-200911_date").value;
if(strDate !== "")
{
  var oDate = util.scand("d mmmm yyyy",strDate);
  if(oDate)
  {
    // Calculate 30 days in milliseconds
    var Dday = 30 * 24 * 60 * 60 * 1000;
    // Create a new date object so we can format it
    oDate = new Date(Dday + oDate.getTime());
    event.value = util.printd("d mmmm yyyy", oDate);
  }
}

But I didn't understand how I can add one month instead of 30 days, since result isn't exactly the same and I need the day to always be the same : only the month number (and the year if needed) must change…

Can someone help me ?
Please, please, please…

[i](Please, be lenient since english isn't my native langage...)[/i]
;-)

My Product Information:
Acrobat Pro 9.2, Macintosh
try67
Expert
Registered: Oct 30 2008
Posts: 2398
In this case it would be better to use setMonth(), instead of the setTime() method. Try this:
oneMonthFromNow = new Date();oneMonthFromNow.setMonth(oneMonthFromNow.getMonth()+1);

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
Thanks try67, but how can I integrate your code inside mine ?

I'd tried, but I just get errors…
My JavaScript knowledge is too limited.
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
What errors are you getting?

If you want to copy and use limited editing for each field:
event.value = "";var iAddMonths = 1; // change for months to add to date// Get the string date from the current date fieldvar strDate = this.getField("prelev.IDservices_form-200911_date").valueAsString;if(strDate !== ""){var oDate = util.scand("d mmmm yyyy",strDate); // the date objectvar iFullYear = oDate.getFullYear(); // full yearvar iMonth = oDate.getMonth();  // zero based monthvar iDate = oDate.getDate();  // date// new date from the year, (month + AddMonths), and  datevar oNewDate = new Date(iFullYear, (iMonth + iAddMonths), iDate);event.value = util.printd("d mmmm yyyy", oNewDate);}

Since there is very little editing that needs to be done, one could use a loop with calculated to function to perform this calculation on many fields.

George Kaiser

Merlin
Acrobat 9ExpertTeam
Registered: Mar 1 2006
Posts: 766
Thanks, thanks, thanks, gkaiseril !

:-)