Copy link to clipboard
Copied
Thanks entirely to the examples and advice from these forums, I am able to extrapolate the actual numeric values of four differing date fields which has enabled me to address a number of problems in other forms. Thank you to everyone and especially the SME's.
Now I find myself failing to find any examples of a script -- or further directions to research -- that will automate determination of the shortest period between Date1 and Dates 2, 3, or 4 then display the differential between Date1 and the date that occurs soonest between Date2, Date3, or Date4 in a fifth text field.
Is it possible to automate the comparison between a beginning date (Date1) and three differing dates (Dates 2, 3, & 4) to determine which is least in order to display only the shortest period of time in days? The answer is to be displayed in a fifth text box "days_lost" where I am working the script in the custom calculation field. Again, my sincerest thanks in advance for any and all assistance.
This is what I have thus far:
var sDate1= new Date(this.getField("Date_Discharge").value);
var sDate2= new Date(this.getField("Date_Articles_Terminated").value);
var sDate3= new Date(this.getField("Date_Voyage Termination").value);
var sDate4= new Date(this.getField("Date_FFD").value);
var date1 = new Date(sDate1.getFullYear(), sDate1.getMonth(), sDate1.getDate());
var date2 = new Date(sDate2.getFullYear(), sDate2.getMonth(), sDate2.getDate());
var date3 = new Date(sDate3.getFullYear(), sDate3.getMonth(), sDate3.getDate());
var date4 = new Date(sDate4.getFullYear(), sDate4.getMonth(), sDate5.getDate());
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = date2.getTime() - date1.getTime();
var millisBetween = date3.getTime() - date1.getTime();
var millisBetween = date4.getTime() - date1.getTime();
var days = millisBetween / millisecondsPerDay;
var eDays = Math.floor(days);
event.value = eDays *1;
Copy link to clipboard
Copied
There are multiple errors in your code, but the answer to your question is yes.
You can use the Math.min function for that. It takes as input any number of values, and returns the lowest one.
In your case that would be something like this:
var minDate = Math.min(date1.getTime(), date2.getTime(), date3.getTime(), date4.getTime());
Copy link to clipboard
Copied
Got it working with this:
function calculateDays(d1, d2){
var date1 = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate());
var date2 = new Date(d2.getFullYear(), d2.getMonth(), d2.getDate());
var millisecondsPerDay = 1000 * 60 * 60 * 24;
var millisBetween = date2.getTime() - date1.getTime();
var days = millisBetween / millisecondsPerDay;
return Math.floor(days);
}
function calculateMin(ddif0, ddif1, ddif2){
return Math.min(ddif0, ddif1, ddif2);
}
var date1 = util.scand("dd mmm yy",this.getField("Date_Discharge").value);
//new Date("2023-01-01");
var date2 = util.scand("dd mmm yy",this.getField("Date_Articles_Term").value);
//new Date("2023-06-03");
var date3 = util.scand("dd mmm yy",this.getField("Date_Voyage_Term").value);
//new Date("2023-04-01");
var date4 = util.scand("dd mmm yy",this.getField("Date_FFD").value);
//new Date("2023-05-01");
var ddif0 = calculateDays(date1, date2);
var ddif1 = calculateDays(date1, date3);
var ddif2 = calculateDays(date1, date4);
var arr = [ddif0,ddif1,ddif2];
event.value= calculateMin(ddif0, ddif1, ddif2);
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more