I have a PDF form with 6 different time periods for each day of the month and I need to make sure those time periods do not overlap when the user completes them. Not all time periods may be completed, but the ones that do must be unique. I have taken inspiration from this previous post Use JavaScript to compare time ranges specifically the contribution by @RobG. However, my lack of scripting knowledge is hampering my ability to mold that code for my specific application. In addition to have the time periods verified, I also have to repeat this for every day on the form but I would like to do it one day at a time particularly when the entries are made for that day. Since this is a PDF form, I am not certain how to execute that validation. For example, should it execute every time a time period is completed? That is, have a Start Time and and End Time? So it would act as a "validation" script for that time entry? Here is a sample of what the form looks like:
Here is my attempt at updating the code for my form:
var TimeIn1 = this.getField("Day1Pd1TimeInRes").value;
var TimeOut1 = this.getField("Day1Pd1TimeOutRes").value;
var TimeIn2 = this.getField("Day1Pd2TimeInRes").value;
var TimeOut2 = this.getField("Day1Pd2TimeOutRes").value;
var TimeIn3 = this.getField("Day1Pd3TimeInRes").value;
var TimeOut3 = this.getField("Day1Pd3TimeOutRes").value;
var TimeIn4 = this.getField("Day1Pd4TimeInRes").value;
var TimeOut4 = this.getField("Day1Pd4TimeOutRes").value;
var TimeIn5 = this.getField("Day1Pd5TimeInRes").value;
var TimeOut5 = this.getField("Day1Pd5TimeOutRes").value;
var TimeIn6 = this.getField("Day1Pd6TimeInRes").value;
var TimeOut6 = this.getField("Day1Pd6TimeOutRes").value;
var timeSlots = [
{BeginTime: new Date("10/25/2015 " + TimeIn1),
EndTime: new Date("10/25/2015 " + TimeOut1)},
{BeginTime: new Date("10/25/2015 " + TimeIn2),
EndTime: new Date("10/25/2015 " + TimeOut2)},
{BeginTime: new Date("10/25/2015 " + TimeIn3),
EndTime: new Date("10/25/2015 " + TimeOut3)},
{BeginTime: new Date("10/25/2015 " + TimeIn4),
EndTime: new Date("10/25/2015 " + TimeOut4)},
{BeginTime: new Date("10/25/2015 " + TimeIn5),
EndTime: new Date("10/25/2015 " + TimeOut5)},
{BeginTime: new Date("10/25/2015 " + TimeIn6),
EndTime: new Date("10/25/2015 " + TimeOut6)}
];
function slotFits(slot) {
if (timeSlots.length == 0) return true; // If no slots, must fit
return timeSlots.some(function(s, i) {
return (i == 0 && slot.EndTime <= s.BeginTime) || //slot is before all others
(s.EndTime <= slot.BeginTime && !timeSlots[i + 1]) || //slot is after all others
(s.EndTime <= slot.BeginTime && timeSlots[i + 1].BeginTime >= slot.EndTime) // Slot between others
});
}
[{BeginTime: new Date("10/25/2015 " + TimeIn1),
EndTime: new Date("10/25/2015 " + TimeOut1)},
{BeginTime: new Date("10/25/2015 " + TimeIn2),
EndTime: new Date("10/25/2015 " + TimeOut2)},
{BeginTime: new Date("10/25/2015 " + TimeIn3),
EndTime: new Date("10/25/2015 " + TimeOut3)},
{BeginTime: new Date("10/25/2015 " + TimeIn4),
EndTime: new Date("10/25/2015 " + TimeOut4)},
{BeginTime: new Date("10/25/2015 " + TimeIn5),
EndTime: new Date("10/25/2015 " + TimeOut5)},
{BeginTime: new Date("10/25/2015 " + TimeIn6),
EndTime: new Date("10/25/2015 " + TimeOut6)},
].forEach(function(slot, i) {
app.alert('Slot ' + i + ': ' + slotFits(slot));
});
In this sample I spelled out the field names, but because I will be repeating this for up to 31 days at a time, I would like to find a way to create a document script where I can put in the 'TimeIn' and 'TimeOut variables and call them for each day, but I think I can get away without that if necessary, just concerned about form size repeating that much code for 6 periods for potentially 31 days. Thank you!
Edit: Time values are HH:mm