Skip to main content
Inspiring
March 10, 2024
Question

Calculating date / time to trigger an if statement

  • March 10, 2024
  • 3 replies
  • 985 views

I need some help working out the following  code. It is suppose to:

1. Check the Arrival and Departure date - if they are different - then it will caculate the number of days between the two dates.

2. If the Arrival and Departure dates are the same, it should compare the Arrival and departure times.

3. If the time difference is greater than 4 hours it should return a value of 1.

4. if the time difference is less than 4 hours it should return a value of 0.

The code does work for the most part, but does not return the correct value when comparing the time difference. - it returns a value of 1 whether the time difference is either greater or less than 4 hours.

 

// Work Area for Driver time;

// Custom calculate script
(function () {

var sStart1 = getField("ADate1").valueAsString;
var sEnd1 = getField("DDate1").valueAsString;
var atime1 = getField("ATime1").value;
var dtime1 = getField("DTime1").value;
var dStart1, dEnd1, diff1;

if(sStart1 && sEnd1) {
if(sStart1 !== sEnd1){
dStart1 = util.scand("m/d/yy", sStart1);
dEnd1 = util.scand("m/d/yy", sEnd1);
diff1 = dEnd1.getTime() - dStart1.getTime();
event.value = Math.floor(diff1 / 864e5)+1;
}

else {

event.value = "";
if (atime1 && dtime1){

function parseTime(aTime1){

if (aTime1 === '') return null;
var a1 = new Date();
var time = aTime1.match(/(\d+)(:(\d\d))?\s*(p?)/);

a1.setHours( parseInt(time[1]) + ( ( parseInt(time[1]) < 12 && time[4] ) ? 12 : 0) );
a1.setMinutes( parseInt(time[3]) || 0 );
a1.setSeconds(0, 0);
return a1;
}

if (atime1 !== "" && dtime1 !== "") {

var atime1a = parseTime(atime1);
var dtime1a = parseTime(dtime1);
var time = (dtime1a - atime1a)/(1000*60*60);
var four = (1000*60*60*4);

if (time => four){
event.value = "1";
}
else{
event.value = "0";
}

}

}}}


else {
event.value = "";
}})();

    This topic has been closed for replies.

    3 replies

    Bernd Alheit
    Community Expert
    Community Expert
    March 11, 2024

    Check the Javascript console for errors.

    DuquetteAuthor
    Inspiring
    March 28, 2024

    Turns out the solution was simple: instead of using a variable (four) expressed in milliseconds to express 4-hours, I just used the number 4 .

     

    if (tot1 >= 4){
    event.value = "1";
    }
    else{
    event.value = "0";
    }

    Bernd Alheit
    Community Expert
    Community Expert
    March 10, 2024

    Use

    if (time >= four){

    DuquetteAuthor
    Inspiring
    March 10, 2024

    Bernd, Thank you for checking this for me. I have to be missing something  else and my understanding parsetime functions is very limited. Changing the operators around changed the event.value to "0"  whether the time difference is greater / less than four hours.

    kglad
    Community Expert
    Community Expert
    March 10, 2024

    in the future, to find the best place to post your message, use the list here, https://community.adobe.com/

    p.s. i don't think the adobe website, and forums in particular, are easy to navigate, so don't spend a lot of time searching that forum list. do your best and we'll move the post (like this one has already been moved) if it helps you get responses.



    <"moved from using the community">