This discussion is archived
5 Replies Latest reply: Oct 11, 2013 5:25 AM by backintheday RSS

Dynamic Actions - Fire When Date Less Than Todays Date

paulgallstar Newbie
Currently Being Moderated

Hello,

 

I've tried doing a search on the forum but its coming a blank.

 

What I am trying to do is I think simple but for some reason I can't figure it out. What I want to do is display a field on my form when the date the user enters is less than todays date. I have a format mask on my date field to the UK standard of DD/MM/YYYY.

 

I have tried to do this using a dynamic action but what I don't really know is how do I take the date that is a text field and convert it back into a date and check against sysdate using a dynamic action? Can this be done with a dynamic action?

 

Cheers,

Paul.

  • 1. Re: Dynamic Actions - Fire When Date Less Than Todays Date
    Hari_639 Guru
    Currently Being Moderated

    Hello Paul,

     

    You have to create a Dynamic Actions which fires on Change of Date item. Set this DA to execute following JS code (Advanced DA)

     

    // replace P1_DATE_ITEM with actual date item name
    // Works for Date Format DD/MM/YYYY
    var itemVal = $v("P1_DATE_ITEM");
    var year = itemVal.substr(6,4);
    var month = itemVal.substr(3,2);
    var day = itemVal.substr(0,2);
    // Construct JS Date Object
    var inputDate =new Date();
    inputDate.setFullYear(year,month-1,day);
    // Get todays date, as Date Object
    var todayDate = new Date();
    // replace P1_SHOW_HIDE_ITEM with YOUR item name
    // Compare input date and today date
    if (inputDate < todayDate)
      $x_ShowItemRow("P1_SHOW_HIDE_ITEM");
    else
      $x_HideItemRow("P1_SHOW_HIDE_ITEM");
    

     

    This should do the needful

     

    Regards,

    Hari

  • 2. Re: Dynamic Actions - Fire When Date Less Than Todays Date
    backintheday Explorer
    Currently Being Moderated

    Paul,

     

    Another option would be to create a hidden page item with a PL/SQL Expression source, P4_SYSDATE with the correct date format:

     

    TRUNC(SYSDATE)

     

    Then, in your Dynamic Action with a Change event on P4_DATE_FIELD (this item also having the correct date format), the condition could be a Javascript Expression:

     

    $v('P4_DATE_FIELD') < $v('P4_SYSDATE')

     

    The true action could then be to show the page item you need to see, P4_DISPLAY_AFTER_ACTION.

     

    I have a demo setup at http://apex.oracle.com/pls/apex/f?p=45550

    workspace: work_ws

    username: demo

    password: demo

    application: Application1

    tab: ActionOnDate

     

    -Seth.

  • 3. Re: Dynamic Actions - Fire When Date Less Than Todays Date
    Hari_639 Guru
    Currently Being Moderated

    Hi,

     

    $v returns string. So you are doing string comparison, not dates.

     

    Try to compare today's date 10/11/2013 with 12/12/2011.

     

    Regards,

    Hari

  • 4. Re: Dynamic Actions - Fire When Date Less Than Todays Date
    paulgallstar Newbie
    Currently Being Moderated

    Hi Hari,

     

    Thanks for that you have put me on the right course. I tweaked it a little as date uses the time part as well so the date input was always different from the current date by a matter of seconds so I used setHours on both dates to 0 before I did my comparison.

     

    Here is my code:

     

    // Get the date string from the input type
    var itemVal = $("#P5_DATE_ITEM").val();  
    //Get the various element of the date ie year, month and date
    var year = itemVal.substr(6,4);  
    var month = itemVal.substr(3,2);  
    var day = itemVal.substr(0,2);  
    // Construct JS Date Object  
    var inputDate =new Date();  
    inputDate.setFullYear(year,month-1,day);  //Months in javascript start from 0
    inputDate.setHours(0,0,0,0);
    console.log('The input date is: ' + inputDate);
    // Get todays date, as Date Object  
    var todayDate = new Date();  
    todayDate.setHours(0,0,0,0);
    console.log('The todays date is: ' + todayDate);
    // replace P1_SHOW_HIDE_ITEM with YOUR item name  
    // Compare input date and today date  
    if (inputDate < todayDate)  {
        $("#P5_ITEM_TO_DISPLAY").show();  
        $('label[for="P5_ITEM_TO_DISPLAY"]').show()
        console.log('In the show part of the if statement.');
    } else  {
        $("#P5_ITEM_TO_DISPLAY").hide();  
        $('label[for="P5_ITEM_TO_DISPLAY"]').hide()
        console.log('In the hide part of the if statement.');
    }

     

    Thanks backintheday for your solution too.

     

    Have a good weekend,

    Paul.

  • 5. Re: Dynamic Actions - Fire When Date Less Than Todays Date
    backintheday Explorer
    Currently Being Moderated

    Good point, Hari. That comparison was not successful as you suggested.

     

    -Seth.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points