Expand my Community achievements bar.

# Adobe Experience Manager Forms

SOLVED

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

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

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```
3 Replies

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.

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```

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":
break;

case "2":
break;

case "3":
break;

case "4":
break;

case "5":
break;

case "6":
break;

case "7":
break;

case "8":
break;

case "10":
break;

case "11":
break;

case "12":
break;
}