Comparing dates using Dynamic Action on DatePicker

2019-08-15 11:18发布

I have a date picker where the user simply chooses a date then a Dynamic Action is suppose to send an alert if the user clicks tomorrow(sysdate+1).

The Datepicker term is the simple layout.

The Dynamic Action-->

Name: Valid Date

Event: Change

Selection type: Item(s)

Item(s): datepicker_name

Condition: equal to

Value: sysdate+1

When I run the program and click any day on the calendar, no alert comes up. I thought the problem was the format. The Dynamic Action sees the date as "DD/MM/YYYY" while the Datepickers output is "DD-Mon-YY" so it could not compare them. Apples and Oranges. But I played around with the format to make it all the same but still no progress.

Thanks again for your time and help!

1条回答
The star\"
2楼-- · 2019-08-15 11:59

As @ScottWe mentions: you're trying to apply PLSQL logic in HTML/javascript. The 'When - Condition' is evaluated at runtime and thus you can't use PLSQL there. The date arithmetic is a bit annoying in javascript though, so if you're a unfamiliar with it, here is a way you can perform your check (which is, is the entered date tomorrow or not).

Taking my clues from these:
Date difference in Javascript (ignoring time of day)
JavaScript how to get tomorrows date in format dd-mm-yy

Add this function to the page's javascript section for global variables and functions:

function isTomorrow(pDateItem){  
  function getTomorrow(){ 
    var tomorrow = new Date();
    tomorrow.setDate(tomorrow.getDate() + 1);
    return tomorrow;
  };

  function cutTime(pDate){
    return new Date(pDate.getFullYear(), pDate.getMonth(), pDate.getDate());
  };

  // check if pDateItem leads to a selection
  // check if it is a datepicker
  // check if a date has been selected
  if ( $(pDateItem).length 
       && $(pDateItem).data("datepicker")
       && $(pDateItem).datepicker("getDate") !== null 
     ) 
  {        
    var tomorrow = getTomorrow();
    var check = $(pDateItem).datepicker("getDate");
    var one = cutTime(check);
    var two = cutTime(tomorrow);

    return one.getDate() === two.getDate();
  };
  return false;
}

Then in your Dynamic action 'When' condition, use a javascript expression with this code:

isTomorrow(this.triggeringElement)

Then the corresponding True Actions will only fire when the date is set to tomorrow.

查看更多
登录 后发表回答