Expand my Community achievements bar.

SOLVED

Need to identify year based on a month/day/year field

Avatar

Level 5

I have a form I'm trying to prefill the Year field based on a month/day/year field.  Not sure of the coding and where it should go.

 

Actually need it defined as such....

 

if EndDate field is after 9/30/22 and before 10/1/23 then year = 2023

1 Accepted Solution

Avatar

Correct answer by
Level 10

Oh, sorry. I overlooked you last sentence. 

To accomplish this, you'll need antother script. However, FormCalc makes data calculations a breeze.

Put this in the exit event of the "To" field.

if (not $.isNull) then
	var n = Date2Num($.formattedValue, "MM/DD/YY")
	var d = Num2Date(n, "D")
	var m = Num2Date(n, "M")
	var y = Num2Date(n, "YY")
	if (y eq 22 and m ge 10 or y eq 23 and m le 9) then
		year = "2023"
	else
		year = "–"
	endif
endif

View solution in original post

3 Replies

Avatar

Level 10

Hi,

 

with the scripting language FormCalc this is very easy to do.

Assuming you want populate the field "Year" in the second row of your form after selecting a date in the field "From" all you need the following script in the exit event of "From".

 

if (not $.isNull) then
	year.rawValue = Num2Date( Date2Num($.formattedValue, "MM/DD/YY"), "YYYY")
endif

In your current form, you'll also have to remote the display pattern text{OO-99-9999} from "Year", since this doesn't match with a year number.

 

 

Avatar

Correct answer by
Level 10

Oh, sorry. I overlooked you last sentence. 

To accomplish this, you'll need antother script. However, FormCalc makes data calculations a breeze.

Put this in the exit event of the "To" field.

if (not $.isNull) then
	var n = Date2Num($.formattedValue, "MM/DD/YY")
	var d = Num2Date(n, "D")
	var m = Num2Date(n, "M")
	var y = Num2Date(n, "YY")
	if (y eq 22 and m ge 10 or y eq 23 and m le 9) then
		year = "2023"
	else
		year = "–"
	endif
endif

Avatar

Level 5

This is exactly what I needed!  Thank you very much.

 

To continue, how would I be able to include the following js code that currently works separate from the code above?  If the year field is 2022 the following needs to happen in a field called 'Campaign' to update a field called 'Enforcement Type'?

EnforcementType.clearItems();
switch (this.rawValue) {

case "1":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
EnforcementType.addItem("Speed Enforcement");
break;

case "2":
EnforcementType.addItem("Occupant Protection");
break;

case "3":
EnforcementType.addItem("Distracted Driving");
break;

case "4":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "5":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "6":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "7":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "8":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "10":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "11":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;

case "12":
EnforcementType.addItem("Alcohol");
EnforcementType.addItem("Occupant Protection");
break;
}