Expand my Community achievements bar.

## how to calculate future date

Level 2

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

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

function

{

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

{

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

6 Replies

Level 10

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

Level 2

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

Level 2

what would be the correct way to accomplished this?

Level 10

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.

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

Hope this helps

Bruce

Level 2

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);

Stefan

Level 1

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);