0

I have a dynamic action which I want to do a number of checks in a javascript condition before it fires. I want it to only show a region, if a date coming back from the database into a read only field (P4_NEXT_RENEWAL_DATE) is LATER than todays system date.

My oracle table has a date field, being returned into a read only page item:

P4_NEXT_RENEWAL_DATE

This returns a value such as:

Next Renewal Date

16-JUL-2017

I have a dynamic action, which looks as below:

(
(apex.item('P4_RENEWAL_REQUIRED').getValue()=='Yes' && apex.item('P4_RENEWAL_REQUIRED_AGREE').getValue()=='Agree')
||
(apex.item('P4_RENEWAL_REQUIRED').getValue()=='No' && apex.item('P4_RENEWAL_REQUIRED_AGREE').getValue()=='Disagree')
)
&& 
Date.parse(apex.item('P4_NEXT_RENEWAL_DATE').getValue()) > new Date()

it is this last bit which is not working,

&& Date.parse(apex.item('P4_NEXT_RENEWAL_DATE').getValue()) > new Date()

which I was hoping would only show the region if my next renewal date was later than todays date....but it shows the region regardless of if the date is in the past or the future.

I am not sure if my read only field is returning a string version of the date in P4_NEXT_RENEWAL_DATE, or if Apex knows its a date value coming from Oracle, and so javascript treats it as a date value...I have tried a few date.parse() methods but nothing seems to work.

I also tried not using date.parse, and using a straight apex.item.getValue() call which I would have through returned the raw date object, but no luck either:

&& apex.item('P4_NEXT_RENEWAL_DATE').getValue() > new Date()

Any advice appreciated.

Hleb
  • 7,037
  • 12
  • 58
  • 117
smackenzie
  • 2,880
  • 7
  • 46
  • 99
  • I'm not clued up on `Date.parse` but I can tell you that the value held in P4_NEXT_RENEWAL_DATE is a **string** e.g. '16-JUL-2017'. So the question is: can `Date.parse` handle strings in that format? – Tony Andrews Aug 24 '16 at 19:44
  • Why would Oracle take a date value from a date column, and then hold it in a page item which is a bind (?) variable of type string? I tried to split the date into 3 parts using 3 Oracle substrings...and then build a string from the parts "Jul 16, 2017" etc. and still no joy – smackenzie Aug 24 '16 at 20:25
  • Google `Date.parse` and `DD-MMM-YYYY` for suggestions on a solution. `apex.item` is a Javascript function (supplied by Oracle) and all it sees is an input of type "text", it is not looking at a column in the database, so it has no knowledge of it holding a date value. – Tony Andrews Aug 24 '16 at 20:29
  • Hi, if I take the script from here: http://stackoverflow.com/questions/22058822/parse-string-dd-mmm-yyyy-to-date-object-javascript-without-libraries and then change my code to this, it seems to work (thanks): && parseDate(apex.item('P4_NEXT_RENEWAL_DATE').getValue()) > new Date() – smackenzie Aug 24 '16 at 20:51
  • Can you clarify on the item with the date value: when are you providing it a value? Ajax? during page render? Is the value dynamic? Eg why wouldn't it work with the source being computed in an expression so the value would be 1 or 0? – Tom Aug 29 '16 at 05:30

2 Answers2

0

Firstly, I came across this on the MDN documentation for Date.parse.

It is not recommended to use Date.parse as until ES5, parsing of strings was entirely implementation dependent. There are still many differences in how different hosts parse date strings, therefore date strings should be manually parsed (a library can help if many different formats are to be accommodated).

A string representing an RFC2822 or ISO 8601 date (other formats may be used, but results may be unexpected).

and this from the documentation for the Date object.

Note: parsing of date strings with the Date constructor (and Date.parse, they are equivalent) is strongly discouraged due to browser differences and inconsistencies.

I've tested a little in various browsers and Chrome happily handles values in the format of 'DD-MON-YYYY' for Date.parse and the Date contructor, but Firefox and IE do not.

If you want to manipulate/compare dates in JavaScript then I can recommend using the Moment.js date utility library to get around these issues. Either that or work out a way to compare your dates in PL/SQL.

Community
  • 1
  • 1
Drumbeg
  • 1,914
  • 1
  • 15
  • 22
0

you have to pare the new Date to compare with.

apex.item('P4_NEXT_RENEWAL_DATE').getValue() > Date.parse(new Date())