Skip to main content
Known Participant
Oct 30, 2020 8:54 PM UTC
Answered

Auto Fill a Month and year

I want to take the requested date of a form and auto fill boxes 6, 5, 4, 3, 2, 1 month prior to that date. Is this possible? I have tried a few other date formula's and they are not working. I want the requested date to be mm/dd/yy format and the boxes to fill in a mmm-yy format. 

This topic is closed to new replies. Start a new post to keep the conversation going.
try67
try67Correct answer
Community Expert
Nov 3, 2020 1:33 PM UTC

Here is the custom calculation script I am using: 

var requestDateString = this.getField("RequestDate").valueAsString;

if (requestDateString="") event.value = "";

else {

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

d.setMonth(d.getMonth()-6);

event.value = util.printd("mmm-yy", d);

}

I am unable to attach the document because I am on a federal laptop with so many restrictions. When I initially choose the request date the mmm-yy fills automatically as needed but when I change the request date the original mmm-yy box stays the same and does not update with the date change. 


Sorry, my bad. There's a small error in the code I posted before.

Change this line:

if (requestDateString="") event.value = "";

To:

if (requestDateString=="") event.value = "";

10 replies

try67
Community Expert
Community Expert
Oct 30, 2020 9:22 PM UTC

Yes, it's possible. Here's how to do it for the one 6 months prior. You can use that code for the other boxes, with minor adjustments, as their custom calculation script:

 

var requestDateString = this.getField("RequestDate").valueAsString;

if (requestDateString=="") event.value = "";

else {

var d = util.scand("mm/dd/yy", requestDateString );

d.setMonth(d.getMonth()-6);

event.value = util.printd("mmm-yy", d);

}

 

Edit: Small mistake in the code fixed

Translate
Known Participant
Nov 2, 2020 1:07 PM UTC

Thank you this did exactly what I wanted it to do but when I change the date in the form they months in the other boxes do not change. Would this be an added string?

Translate
try67
Community Expert
Community Expert
Nov 2, 2020 1:47 PM UTC

No, if you placed the code where I suggested then it should happen automatically.

Translate