how to calculate future date

Avatar

Avatar

PensFan2011

Avatar

PensFan2011

PensFan2011

21-03-2011

I have used the following code from http://forums.adobe.com/message/2077238#2077238.   But it does not seem to work when straddling years.

I want the use to enter a date in DateTimeField1, then the date in DateTimeField2 to add 6 months to DateTimeField1

Then DateTimeField3 to equal 4 months from DateTimeField 2.  Then DateTimeField4 to add 6 months to DateTimeField3

DateTimeField1 = user enter

DateTimeField2 = util.printd("mm/dd/yyyy", DateUtils.addMonth(new Date(Date.parse(DateTimeField1.formattedValue)),5));

DateTimeField3 = util.printd("mm/dd/yyyy", DateUtils.addMonth2(new Date(Date.parse(DateTimeField2 .formattedValue)),4));

DateTimeField4 = util.printd("mm/dd/yyyy", DateUtils.addMonth(new Date(Date.parse(DateTimeField3.formattedValue)),5));

function

addMonth(date, months)

{

var year = date.getFullYear();

var month = date.getMonth();

var day = date.getDate();

month =  ((month + months - 1) % 12) + 1;

year += parseInt(month / 12);

var days = daysInMonth(month, year);

return new Date(year, month, days);

}

function

addMonth2(date, months)

{

var year = date.getFullYear();

var month = date.getMonth();

var day = 1;

month = ((month + months - 1) % 12) + 1;

year += parseInt(month / 12);

return new Date(year, month, day);

}

function daysInMonth(month, year)

{

return 32 - new Date(year, month, 32).getDate();

}

When I enter 01/01/2010 in DateTimeField1, the following is that results:

DateTimeField2 = 06/30/2010

DateTimeField3 = 10/01/2010

DateTimeField4 = 03/31/2010

The desired result is:

DateTimeField2 = 06/30/2010

DateTimeField3 = 10/01/2010

DateTimeField4 = 03/31/2011

Accepted Solutions (0)

Answers (6)

Answers (6)

Avatar

Avatar

GRUMPY_VISIONAR

Avatar

GRUMPY_VISIONAR

GRUMPY_VISIONAR

30-10-2018

I had the same issue and found that bumping up the increment of the ‘month’ and ‘year’ calculations fixed the issue with jumping to the next year.

var year = date.getFullYear();

var month = date.getMonth();

var day = date.getDate();

               

month = ((month + months - 2) % 13) + 2;

year += parseInt(month / 13);

               

var days = daysInMonth(month, year);

Avatar

Avatar

Town_of_Sandwic

Avatar

Town_of_Sandwic

Town_of_Sandwic

06-05-2013

Hi Bruce,

Thanks for the quick response.  I'm not sure why, but the FormCalc code DateTimeField.rawValue = XFADate.newDate(Date1).addMonths(2).toXFADate() didn't work either.  I ended up getting it to work by placing the following JavaScript code in the calculate field of my second date field:

var examination = new Date(DateField1.formattedValue);

var nextExam = new Date(new Date(examination).setMonth(examination.getMonth()+2));

util.printd("mm/dd/yyyy", nextExam);

Thanks for your help,

Stefan

Avatar

Avatar

_Bruce_Robertson

MVP

Avatar

_Bruce_Robertson

MVP

_Bruce_Robertson
MVP

04-05-2013

Hi Stefan

I think it might depend on your system locale setting.


For date handling I use a script object, I described in the Adobe cookbooks at http://cookbooks.adobe.com/post_Date_handling_in_Livecycle_Designer_ES_forms-19697.html. But the cookbooks seem to be in the middle of being removed, we can no longer add, or update cookbooks and the links are all broken.


So you can download this sample from https://workspaces.acrobat.com/?d=ClBPCI2k5*LlAgUSb-AjXg and see if it will help you.


If you have a DateTimeField to add 2 months to it and assign it to another DateTimeField you could put the following code in the calculate event of the second field


DateTimeField.rawValue = XFADate.newDate(Date1).addMonths(2).toXFADate()

Hope this helps

Bruce

Avatar

Avatar

PensFan2011

Avatar

PensFan2011

PensFan2011

03-05-2013

what would be the correct way to accomplished this?

Avatar

Avatar

Town_of_Sandwic

Avatar

Town_of_Sandwic

Town_of_Sandwic

01-05-2013

Hi Bruce,

I was wondering if this issue has ever been resolved?  The code adds months correctly however, it does not calculate years e.g. one month added to 12/31/2011 should calculate as 01/31/2012. Your suggestion of using rawValue instead of formattedValue returns 01/00/0000 no matter what date is entered.

Thanks,

Stefan

Avatar

Avatar

_Bruce_Robertson

MVP

Avatar

_Bruce_Robertson

MVP

_Bruce_Robertson
MVP

23-03-2011

Hi,  I'm not sure I understand the result required here.  Wont 6 months on from 01/01/2010 be 01/07/2010, then 4 months be 11/01/2010 and another six months be 05/01/2011.  Your desired results have gaps of 180 days, 93 days then 181 days.

Not sure why the script isn't working for you, maybe you should try Date.parse(DateTimeField1.rawValue).  The rawValue will always be in yyyy-mm-dd format, where the formattedValue will vary depending on locale and display patterns.

Regards

Bruce