-2
function CheckDates() {
 var ExtendToDate = GetFieldValue("Extend to Date");
 var LastLeaverDate = GetFieldValue("ARP Mandatory Exit Date");



     var difference = moment.duration(moment(LastLeaverDate, 'MM-DD-YYYY') - 
moment(ExtendToDate, 'MM-DD-YYYY')).asDays();

 if(difference < 30) {
      alert("This persons will have to leave within 30 days of the proposed extension");
 }

}

function CheckDates_WithDelay() { 
// Delay 1000 milliseconds before starting CheckDates
setTimeout("CheckDates()",1500);

}

AddChangeCallback("ARP_MANDATORY_EXIT_DATE", CheckDates_WithDelay);
AddChangeCallback("EXTEND_TO_DATE", CheckDates_WithDelay);

On the form the user can change both the Extend to Date and ARP Exit date. So that is why i added the change call back and the delay. I've tested it numerous times but i get no alert...

Appreciate your help.

Lee S
  • 17
  • 6

2 Answers2

1

I don't know what AddChangeCallback() is and why it changed from "Extend to Date" to "EXTEND_TO_DATE" and I also don't understand why the timeout, but I think it should look like this, assuming the message should appear when ExtendToDate is after LastLeaverDate, but less than 30 days after.

This solution uses http://momentjs.com/ (so you don't have to worry about [strange things][1])

// Observe inputs for a change (I assume)
AddChangeCallback("ARP_MANDATORY_EXIT_DATE", CheckDates);
AddChangeCallback("EXTEND_TO_DATE", CheckDates);

// This will display an alert if ExtendToDate is in the period
// 30 days after LastLeaverDate
function CheckDates() {
     // Get the field values
     var ExtendToDate = GetFieldValue("EXTEND_TO_DATE");
     var LastLeaverDate = GetFieldValue("LAST_LEAVER_DATE");

     // Check if values are present
     if(ExtendToDate.length === 0 || LastLeaverDate === 0) {
          // at least one of the fields is empty -> leave the function
          return false;
     }

     // convert both dates to moment.js-objects [http://momentjs.com/docs/#/parsing/] and check if they're parsable
     var ExtendToDateMoment = moment(ExtendToDate, 'MM-DD-YY');
     if(ExtendToDateMoment.isValid() === false)  {
         return false;
     }

     var LastLeaverDateMoment = moment(LastLeaverDate, 'MM-DD-YY');
     if(LastLeaverDateMoment.isValid() === false)  {
         return false;
     }

     /*
     - subtract them,
     - create a moment.duration-object [http://momentjs.com/docs/#/durations/] and
     - return the difference in days [http://momentjs.com/docs/#/durations/days/]
     */
     var difference = moment.duration(ExtendToDateMoment - LastLeaverDateMoment).asDays();

     // compare the difference to a fixed value
     if(difference < 30) {
          alert("This persons will have to leave within 30 days of the proposed extension");
     }
}
Reeno
  • 5,720
  • 11
  • 37
  • 50
  • Thanks Reeno, i tried both methods and neither seem to work. I actually got the date format incorrect. It is MM-DD-YY, would it be ignorant of me to assume i can just change it reflect that ? – Lee S Sep 08 '15 at 14:54
  • In the first method you can just change the date format in the parameters (change `moment(LastLeaverDate, 'DD-MM-YY')` to `moment(LastLeaverDate, 'MM-DD-YY')`, same for ExtendToDate) – Reeno Sep 08 '15 at 14:56
  • For the second method flip the first and second parameters of the call to `new Date()` (both occurrences) – Reeno Sep 08 '15 at 14:57
  • i was hoping you would say that was not case because i tried that and it still did not work. Both the dates can be updated so i added a changeCallBack and also a time delay to this. – Lee S Sep 08 '15 at 15:04
  • function CheckDates() { var ExtendToDate = GetFieldValue("Extend to Date"); var LastLeaverDate = GetFieldValue("ARP Mandatory Exit Date"); var difference = moment.duration(moment(LastLeaverDate, 'MM-DD-YYYY') - moment(ExtendToDate, 'MM-DD-YYYY')).asDays(); // compare the difference to a fixed value if(difference < 30) { alert("This persons will have to leave within 30 days of the proposed extension"); } } – Lee S Sep 08 '15 at 15:07
  • function CheckDates_WithDelay() { // Delay 1000 milliseconds before starting CheckDates setTimeout("CheckDates()",1500); } AddChangeCallback("ARP_MANDATORY_EXIT_DATE", CheckDates_WithDelay); AddChangeCallback("EXTEND_TO_DATE", CheckDates_WithDelay); – Lee S Sep 08 '15 at 15:07
  • Please add this to your question so it's better readable. I'll have a look later – Reeno Sep 08 '15 at 15:10
  • Cheers Reeno, added to Question – Lee S Sep 08 '15 at 18:09
  • I edited my answer, but some things are still unclear for me – Reeno Sep 08 '15 at 20:28
  • @LeeS Did it help you? – Reeno Sep 14 '15 at 10:20
  • Hi Reeno, Unfortunately not, i think maybe the software i was using didn't like Moment.JS??? Thank you for your help anyway! I have posted my answer which works below – Lee S Sep 15 '15 at 10:33
0
var mS = 1000 * 60 * 60 * 24;

function CheckDates() {

    var a = GetFieldValue("ARP_MANDATORY_EXIT_DATE") ;
    var lastLeaverDate = (Date.parse(a));

    var b = GetFieldValue("EXTEND_TO_DATE");
    var extendToDate = (Date.parse(b));

    var differenceCalc = (lastLeaverDate - extendToDate);

    var difference = (differenceCalc / mS);

    if(difference < 30) {
          alert("This persons will have to leave within 30 days of the proposed extension");
     }
}
Reeno
  • 5,720
  • 11
  • 37
  • 50
Lee S
  • 17
  • 6