Need to identify year based on a month/day/year field | Community
Skip to main content
ReluctantProgrammer
Level 4
Oct 3, 2022 3:10 PM UTC
Solved

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

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
radzmar
radzmarAccepted solution
Level 10
Oct 3, 2022 6:12 PM UTC

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

radzmar
Level 10
Oct 3, 2022 5:59 PM UTC

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.

 

 

Translate
radzmar
radzmarAccepted solution
Level 10
Oct 3, 2022 6:12 PM UTC

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
Translate
ReluctantProgrammer
Level 4
Oct 3, 2022 7:45 PM UTC

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;
}

 

 

 

 

 

 

 

Translate