Skip to main content
Participating Frequently
November 12, 2018
Question

Add X months to a defined date

  • November 12, 2018
  • 1 reply
  • 3583 views

Hello,

I'm a complete novice with forms, but one of my first requirements is to create a form that will calculate and display an date advanced X number of months from a defined date.  For example, I enter 11/12/2018 in the defined date field, 3 in the X  months field and the calculated fields should display 2/12/2019.

After some exhaustive searching in this forum and Google, I've managed to achieve some success.  The following calculates and displays a date advances X number of days:

var originalDateString = this.getField("StartDate").valueAsString;

var daysString = this.getField("Offset").valueAsString; 

if (originalDateString=="" || daysString=="") event.value = ""; 

else { 

    var d = util.scand("mm/dd/yyyy", originalDateString); 

    var days = Number(daysString); 

    d.setDate(d.getDate()+days); 

    event.value = util.printd("mm/dd/yyyy", d); 

I can't figure out how to change from adding days to adding months.  Additionally, from what I did read (but couldn't get to work) this problem is very complex.  At one point, I was able to get close (have lost the link and can't find it now), but is was something like SetMonth.  Well, that worked for my example above, but it the fixed date was 11/30/2018 and X = 3, the return was 03/02/2019 and not 02/28/2019. 

Thanks for any help provided.

This topic has been closed for replies.

1 reply

try67
Adobe Expert
November 12, 2018

Instead of getDate and setDate use getMonth and setMonth. Be aware, though,

that the values range from 0 to 11.

Participating Frequently
November 12, 2018

Thank you.  Yes, GetMonth/SetMonth was what I had tried before and lost.  While that works in some case, it doesn't work (or work as required) in some cases.  For example if the defined date is 11/30/2018 and the X value is 3 the returned value is 03/02/2019 and not 02/28/2109 as required.

I am relatively fluent in VB/VBA programming but I know nothing about Java.  I have a function that works and returns the required date in cases like the above:

Sub Test()

  MsgBox AdvanceDateByXMonths("11/30/2018", 3)

End Sub

Function AdvanceDateByXMonths(oDate As Date, Offset As Long) As Date

Dim AdvDay As Long, AdvMonth As Long, AdvYear As Long

AdvMonth = Abs(Month(oDate) + Offset + 11) Mod 12 + 1

AdvYear = Int(Year(oDate) + (Offset + Month(oDate) - 1) / 12)

Select Case AdvMonth

  Case 2

    If Day(oDate) > 28 Then

      If (AdvYear Mod 4 = 0) Or ((AdvYear Mod 400 = 0) And (AdvYear Mod 100 <> 0)) Then

        AdvDay = 29

      Else

        AdvDay = 28

      End If

    Else

      AdvDay = Day(oDate)

    End If

  Case 4, 6, 9, 11

    If Day(oDate) > 30 Then

      AdvDay = 30

    Else

      AdvDay = Day(oDate)

    End If

  Case Else: AdvDay = Day(oDate)

End Select

AdvanceDateByXMonths = AdvDay & "/" & AdvMonth & "/" & AdvYear

End Function

Is there an equivalent Java function? If so, how would I employ it in my custom script above.  Thank you!!

try67
Adobe Expert
November 12, 2018

".....then write the code to do it in that way"

Well, therein lies the problem.  While I know the logic (in VB/VBA) to accurately return an offset month and account for differences in number of days in a month and leap years, I dodn't know how to write or employ an equivalent JavaScript function to include in the field custom calculation expression. That is why I posted here looking for help.


These tutorials will help you get started:

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript

https://acrobatusers.com/tutorials/working-with-date-and-time-in-acrobat-javascript-part-2

https://acrobatusers.com/tutorials/working-date-and-time-acrobat-javascript-part-3-3