Skip to main content
Participating Frequently
August 29, 2020
Question

javascript populate fields based on date

  • August 29, 2020
  • 1 reply
  • 1025 views

In my AcroForm

I have a date field (DateOfInsp)

 

I have a Fiscal Year field (FY) and a Quarter field (Qtr)

 

I want to populate the FY (yyyy) field and the Qtr field (1,2,3,4) based on the date field

 

The fiscal year for my organization starts 10-1 and ends 9-30 of the following year

 

I am looking for the javascript to do this, or guidance so I can learn myself...I am pretty new and just want to automate my forms as much as possible and provide data validation whenever possible

 

I have seen many posts related to this, but haven't figured it out yet...

 

Any help is greatly appreciated

 

 

This topic has been closed for replies.

1 reply

try67
Community Expert
Community Expert
August 29, 2020

What are the start and end dates of each quarter? So if the date is before September 30th it's considered to be in the fiscal year that is one before the calendar year? For example, 09-01-2020 is in Q4 of 2019?

Participating Frequently
September 1, 2020

In your example above...09-01-2020 is Q4 of FY2020

So

10-01-2020 thru 12-31-2020 Q1 FY2021

01-01-2021 thru 03-31-2021 Q2 FY2021

04-01-2021 thru 06-30-2021 Q3 FY2021

07-01-2021 thru 09-30-2021 Q4 FY2021

try67
Community Expert
Community Expert
September 1, 2020

OK, then you can use this code as the custom calculation script of the "Qtr" field:

 

var dateOfInspString = this.getField("DateOfInsp").valueAsString;
if (dateOfInspString=="") {
	event.value = "";
	this.getField("FY").value = "";
} else {
	var dateOfInsp = util.scand("mm-dd-yyyy", dateOfInspString);
	if (dateOfInsp.getMonth()>=9) {
		event.value = "4";
		this.getField("FY").value = dateOfInsp.getFullYear()+1;
	} else {
		if (dateOfInsp.getMonth()>=6) event.value = "3";
		else if (dateOfInsp.getMonth()>=3) event.value = "2";
		else event.value = "1";
		this.getField("FY").value = dateOfInsp.getFullYear();
	}
}