// Create variables for all the fields you're going to access
var startField = Context.GetField("Start Date") ;
var endField = Context.GetField("End Date");
var durationField = Context.GetField("Duration");
var startDate = startField.Value;
var endDate = endField.Value;
//Check if the "Start Date" and "End Date" fields were found on the document
if (endField && endDate && startField && startDate)
{
//Calculate the sick leave duration
var length = (1 + (endDate.getTime() - startDate.getTime()) / 3600000 / 24);
//If the duration field was not found or could not be parsed as a number, pass the calculated value to the field
if (!durationField.Value)
durationField.Value = length;
//If the duration field was found, compare its value with the calculated duration
else if (durationField.Value != length)
{
Context.CheckSucceeded = false;
Context.ErrorMessage = "\"Duration\"字段的值与实际病假持续时间不匹配";
durationField.AddSuggestion(length.toString());
}
}