Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Instead of getDate and setDate use getMonth and setMonth. Be aware, though,
that the values range from 0 to 11.
Copy link to clipboard
Copied
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!!
Copy link to clipboard
Copied
Adding full calendar months is very tricky, as you've seen. You need to define for yourself how you want it to work in those cases where the results is not obvious, and then write the code to do it in that way.
Copy link to clipboard
Copied
".....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.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
try67,
Thanks for your participation, but none of those links brings me any closer to a solution. The solution is a) if one exists or can be created, a JavaScript function that performs the same function as the VB function I posted. and b) knowledge on how to employ that function in a Adobe custom calculation.
e.g., event.value = (the returned value of a JavaScript function that accurately projects X months in advance)
or that might be:
var originalDateString = this.getField("StartDate").valueAsString;
var monthsString = this.getField("Offset").valueAsString;
if (originalDateString=="" || monthsString=="") event.value = "";
else {
var d = util.scand("mm/dd/yyyy", originalDateString);
var months = Number(monthsString);
d.setDate(value of some JavaScript function with d and months passed as arguments);
event.value = util.printd("mm/dd/yyyy", d);
}
Copy link to clipboard
Copied
There's no built-in command in Acrobat or JavaScript that will do that.
If you want someone to write the code for you (for a fee), feel free to contact me privately via try6767 at gmail.com .
Copy link to clipboard
Copied
Thanks for the offer but no thank you. If I had a dollar for every VBA code snippet that I've posted in various Office forums over the years I would really be retired. That said, I understand every person has to eat. It just isn't that important to me. Thanks again for your replies.
Copy link to clipboard
Copied
I looked over your code, and if you could write that in VB (which is a not a very good language) then you could write the same thing in JavaScript (not Java, which is a completely different thing). JavaScript is an easy language to learn, it's well constructed, and has all the features you need to convert your code almost one for one.
Copy link to clipboard
Copied
Thom,
You are probably right, but I confess that I came here hoping to be given the fish ;-). I am almost 60 years old and this little project was taken on a as a favor. I just don't have the interest or time for that matter to roll up my sleeves and learn a new (if even better) language. I wouldn't even know where to begin or even step up to the plate. I don't know what a JavaScript Editor is or where to find it.
I'm not asking for someone to write my code for me (I do that enough and I know it is frustrating). I was simply hoping that there was a function someone had written to do this and for some guidance on how to set the value of the Adobe custom calculation event.value to the value of that function.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now