Expand my Community achievements bar.

SOLVED

Auto calculate end date

Avatar

Level 2

I have already a lot of different pdf forms used to register learners as they start their apprenticeship with my company. Different programs mean different length. This is more a personal habit to try to see if I can make it. I managed to make a commercial registration field with buttons to add and remove lines and fields hidden unless corresponding radio buttons are checked.

 

Long story short, I have this ambition of having a first form where people feed in information like "Learner's name", "Coach", Start Date", choose the programme from the drop down list and click a button and corresponding forms are generated.

 

1. My question is how do I calculate the EndDate of 18 months minus 1 day from the StartDate. I tried using the script that works on Adobe Pro for my current forms but it doesn't work.

 

var date= util.scand("dd/mm/yyyy", this.getField("StartDate").value);
date.setMonth(date.getMonth()+18)
date.setDate(date.getDate()-1)
if (this.getField("StartDate").value!="")
{
event.value=util.printd("dd/mm/yyyy",date)
}
else
{event.value=""}

 

2. My second question is if I have different lengths of programs of 13-15 months, how do I improve the script to reflect that?

 

Many thanks in advance

Thao

1 Accepted Solution

Avatar

Correct answer by
Level 2

I actually found out the answer myself, 18 months = 548 days, 13 months = 396 days, 15 months = 457 days. I tried it many times and the numbers are correct so I simpled it down to the following code:

 

var date = Date2Num(StartDate.formattedValue,"DD/MM/YYYY")
EndDate.formattedValue = Num2Date(date+548,"DD/MM/YYYY")

 

I am stumped again on the drop down list and will post the question in a separate post.

View solution in original post

9 Replies

Avatar

Level 10

Hi there,

 

First let's try to find a solution for your first issue where you can't change the date.

When you were using this code to change the date within Adobe Pro, you were certainly creating Static PDFs which is using a different syntax than Dynamic PDF.

So my question to you, is do you know if you still are working with Static PDFs or is this a Dynamic PDF?

 

If you were to display the field's value, when it is not null, using the following:

app.alert(this.getField("StartDate").value);

 

What is the value?

If it doesn't show any value this means there's an error which probably means you are creating a Dynamic PDF.

Avatar

Level 2

Yes. I want to create a Dynamic pdf. I didn't realize the syntax is different from the Adobe Pro one. I have been researching a couple of days now but lots of questions are about adding days in and what I am looking for is for the EndDate to show the date before 18 months from the start date, i.e Start Date = 26/03/2020 --> EndDate = 25/09/2021.

 

Many thanks

Avatar

Level 10

Alright, well first with dynamic PDFs you will have to forget all you know about using this.getField(), this does not work anymore, with some exceptions.

If you wish to retrieve the value of a field you must use the xPath or somExpression of a field to be able to access it.

While your text cursor is in the JavaScript editor, you can use the Ctrl+LeftClick command on any object in the design to automatically write the reference syntax for you.

While in the event of the object that you want to access, you can use the common "this" to easily access that same object.

To be able to retrieve the value of a field, you will now have to use the "rawValue" property. There's also the option to use "formattedValue" property or even "value.[oneOfChild].value".. the last option is getting more in details.

 

Now to be able to get to change the date like you wish to do, there's a lot of documentation on the matter, if you are familiar with script objects you can easily use the XFADate which Bruce (Br001) has developed years ago which ease a lot the date handling with so many functions pre-established. You can study how functions are used and use it to your own benefits. In the long term it's really useful.

http://adobelivecycledesignercookbookbybr001.blogspot.com/2013/05/date-handling-in-livecycle-designe...

 

If you wish to write down your own JavaScript here is a good way which you can do it:

// form1.Page1.dateStart::exit - (JavaScript, client)

//Let's say the event in which you write this would be the exit event of the start date field
//Access a field's value using the "rawValue" property
if (this.rawValue != null){
   //Date format of "rawValue" to be assumed as (YYYY-MM-DD)
   //Split Year/Month/Day in an array
	var arrDates = this.rawValue.split("-");
   //Create a new date object with the parameters as following (Year, Month, Day)
   //Month are zero based index when creating a new Date object (-1 is required)
	var date = new Date(arrDates[0], arrDates[1]-1, arrDates[2]);
   //Same old code
	date.setMonth(date.getMonth() + 18);
	date.setDate(date.getDate() - 1);
	dateEnd.rawValue = util.printd("yyyy-mm-dd", date);
}

 

I hope this will help.

Avatar

Level 10

Alright, well first of all you'll have to forget all you know about this.getField() method when using Dynamic Fields.

Dynamic forms are using XPath or "somExpression" to access different fields. You can access any field's path by having your text cursor in the JavaScript editor and then use the Ctrl+Left_Click on any object in the design and this will insert the reference syntax based on the current object's event you are accessing the wanted object.

You can also use the common "this" to access the same object of the event that you are in.

Any value of a field can now be accessed using the rawValue property. You can also use the formattedValue property to retrieve the displayed value to the user.

 

Back to the date issue, there's plenty of documentation that can help you achieved such a thing on the web. One of the best tools that I've found is using a Script Object which Bruce (BR001) has developped. This will provide many different pre-established functions to use for dates. In the long run, this script object is very useful for general use. Take a bit of time to look how the script object is being used and you will be able to handle dates really easily.

http://adobelivecycledesignercookbookbybr001.blogspot.com/2013/05/date-handling-in-livecycle-designe...

 

If you'd like to create your own JavaScript to achieve it, here's one way on how you can do it:

// form1.Page1.dateStart::exit - (JavaScript, client)

//This code is within the exit event of the start date field
//Always verify that the rawValue is different than null when handling it with functions
if (this.rawValue != null){
   //Separate the date's value into an array assuming the rawValue's format is (YYYY-MM-DD)
	var arrDates = this.rawValue.split("-");
   //Creating a new date object with the following parameters (Year, Month, Day)
   //Month parameter is zero based index when creating new Date() object, therefor -1 is needed
	var date = new Date(arrDates[0], arrDates[1]-1, arrDates[2]);
   //Same old code
	date.setMonth(date.getMonth() + 18);
	date.setDate(date.getDate() - 1);
	dateEnd.rawValue = util.printd("yyyy-mm-dd", date);
}

 

 

I hope this will help.

Let me know if you need more assistance for this issue or the next step regarding different lengths of program

Avatar

Level 10

Alright, well first of all you'll have to forget all you know about this.getField() method when using Dynamic Fields.

Dynamic forms are using XPath or "somExpression" to access different fields. You can access any field's path by having your text cursor in the JavaScript editor and then use the Ctrl+Left_Click on any object in the design and this will insert the reference syntax based on the current object's event you are accessing the wanted object.

You can also use the common "this" to access the same object of the event that you are in.

Any value of a field can now be accessed using the rawValue property. You can also use the formattedValue property to retrieve the displayed value to the user.

 

Back to the date issue, there's plenty of documentation that can help you achieved such a thing on the web. One of the best tools that I've found is using a Script Object which Bruce (BR001) has developped. This will provide many different pre-established functions to use for dates. In the long run, this script object is very useful for general use. Take a bit of time to look how the script object is being used and you will be able to handle dates really easily.

 

If you'd like to create your own JavaScript to achieve it, here's one way on how you can do it:

// form1.Page1.dateStart::exit - (JavaScript, client)

//This code is within the exit event of the start date field
//Always verify that the rawValue is different than null when handling it with functions
if (this.rawValue != null){
   //Separate the date's value into an array assuming the rawValue's format is (YYYY-MM-DD)
	var arrDates = this.rawValue.split("-");
   //Creating a new date object with the following parameters (Year, Month, Day)
   //Month parameter is zero based index when creating new Date() object, therefor -1 is needed
	var date = new Date(arrDates[0], arrDates[1]-1, arrDates[2]);
   //Same old code
	date.setMonth(date.getMonth() + 18);
	date.setDate(date.getDate() - 1);
	dateEnd.rawValue = util.printd("yyyy-mm-dd", date);
}

 

I hope this will help.

Let me know if you need more assistance for this issue or the next step regarding different lengths of program

Avatar

Level 10

Alright, well first of all you'll have to forget all you know about this.getField() method when using Dynamic Fields.

Dynamic forms are using XPath or "somExpression" to access different fields. You can access any field's path by having your text cursor in the JavaScript editor and then use the Ctrl+Left_Click on any object in the design and this will insert the reference syntax based on the current object's event you are accessing the wanted object.

You can also use the common "this" to access the same object of the event that you are in.

Any value of a field can now be accessed using the rawValue property. You can also use the formattedValue property to retrieve the displayed value to the user.

 

Back to the date issue, there's plenty of documentation that can help you achieved such a thing on the web. One of the best tools that I've found is using a Script Object which Bruce (BR001) has developped. This will provide many different pre-established functions to use for dates. In the long run, this script object is very useful for general use. Take a bit of time to look how the script object is being used and you will be able to handle dates really easily.

http://adobelivecycledesignercookbookbybr001.blogspot.com/2013/05/date-handling-in-livecycle-designe...

 

If you'd like to create your own JavaScript to achieve it, here's one way on how you can do it:

// form1.Page1.dateStart::exit - (JavaScript, client)

//This code is within the exit event of the start date field
//Always verify that the rawValue is different than null when handling it with functions
if (this.rawValue != null){
   //Separate the date's value into an array assuming the rawValue's format is (YYYY-MM-DD)
      var arrDates = this.rawValue.split("-");
   //Creating a new date object with the following parameters (Year, Month, Day)
   //Month parameter is zero based index when creating new Date() object, therefor -1 is needed
      var date = new Date(arrDates[0], arrDates[1]-1, arrDates[2]);
   //Same old code
      date.setMonth(date.getMonth() + 18);
      date.setDate(date.getDate() - 1);
      dateEnd.rawValue = util.printd("yyyy-mm-dd", date);
}

 

I hope this will help.

Let me know if you need more assistance for this issue or the next step regarding different lengths of program

Avatar

Correct answer by
Level 2

I actually found out the answer myself, 18 months = 548 days, 13 months = 396 days, 15 months = 457 days. I tried it many times and the numbers are correct so I simpled it down to the following code:

 

var date = Date2Num(StartDate.formattedValue,"DD/MM/YYYY")
EndDate.formattedValue = Num2Date(date+548,"DD/MM/YYYY")

 

I am stumped again on the drop down list and will post the question in a separate post.

Avatar

Level 10

FormCalc is good way to do it, I almost forgot about it. Although, don't assume months are specific number of days... as of February is not a full month and for the rest there's 7 months with 31 days and 4 months with 30 days... counting months in days may result in errors. It all depends how it should be calculated.

Also, I've sent you a message directly to you because for some reason the forum would not let me insert any new answer.

Avatar

Level 10

Hi,

 

the end date calculation isn't that trivial, since some months have more days than others, so simply adding a fixed amount of days won't work in some circumstances.

Here's a FormCalc script that will do take care of this. 

 

if (HasValue(StartDate) eq 1) then
	var cPattern = "DD/MM/YYYY"
	var nDate = Date2Num(StartDate.formattedValue, cPattern)
	var nStartDate = nDate
	var nEndDate
	var nCountMonths = 18
	var nUltimo
	var nPrevMonth
	var nNextMonth
	
	; Get target month
	while (nCountMonths gt 0) do
	    nPrevMonth = Num2Date(nDate, "M")
	    nDate = Sum(nDate, 1)
	    nNextMonth = Num2Date(nDate, "M")
	    if (nPrevMonth ne nNextMonth) then
		    nCountMonths = Sum(nCountMonths, -1)
		endif
	endwhile
	
	; Get the length of the end month (ultimo)
	nUltimo = nDate
	while (Num2Date(Sum(nUltimo, 1), "M") eq Num2Date(nDate, "M")) do
	    nUltimo = Sum(nUltimo, 1)
	endwhile

	; Set end date
        if (Num2Date(nStartDate, "D") gt Num2Date(nUltimo, "D"))  then
	    ; Set ultimo as end date if end month has less days than start month
	    nEndDate = nUltimo
	else
	    ; Set previous day of start month as day of month in end date
	   	nEndDate = Sum(nDate, Num2Date(Sum(nStartDate, -1), "D"), -1)
	endif
	$ = Num2Date(nEndDate, cPattern)
endif

 

Hope this helps!