DateDiff Javascript - 如果一个日期大于另一个日期,则返回警报

时间:2015-09-02 11:02:52

标签: javascript date-difference

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);

在表单上,​​用户可以更改“延长到日期”和“ARP退出”日期。这就是为什么我添加了更改回调和延迟。我已经多次测试了,但我没有警觉......

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我不知道AddChangeCallback()是什么以及为什么它从"Extend to Date"更改为"EXTEND_TO_DATE"而且我也不明白为什么会超时,但我认为应该看起来像这样,假设当ExtendToDate在之后 LastLeaverDate但是在不到30天之后,该消息应该出现。

此解决方案使用http://momentjs.com/(因此您不必担心[奇怪的事情] [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");
     }
}

答案 1 :(得分: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");
     }
}