Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

Simple date check based on user input

Community Beginner ,
Mar 09, 2020 Mar 09, 2020

Hi all

I've spent 3 days going through various posts and communities and am still stuck.  It's not a difficult problem, but for some reason I cannot figure it out (I blame Daylight Savings Time LOL).

 

I'm using Adobe Acrobat DC and have two dates - Referral Date (ref_date) and Decision to Treat Date (dec_to_treat_date).  All I want is a message (date_message) to show on the form if the Decision to Treat is greater than Referral Date.  Here's my code at the moment, which is in my ref_date field as an ON BLUR action.  For some reason, if I put the two dates in as the same month, it works, but if I put the dates in as different months, nothing happens.  I've tried the Debugger without luck and as I'm brand new to JS, have no idea what other things I should look for.

Any ideas or suggestions would be greatly appreciated.  

Chris 

var d1 = this.getField("dec_to_treat_date").value; 
var d2 = this.getField("ref_date").value; 
var dm = getField("date_message");
if(d1 > d2)
{
dm.display = display.visible; 
}
else
{
dm.display = display.hidden; 
}

 

859
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
Mar 10, 2020 Mar 10, 2020

Use valueAsString instead of value and then use !="" to check if it's not empty.

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2020 Mar 09, 2020

It doesn't work because that's not how you compare dates in JavaScript.

What is the format of your date fields?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2020 Mar 10, 2020

Hi @try67 - they're in dd-mmm-yyyy format.  I can't find the link right now but the structure of the code was posted as a response to a SO question, so I guess that was misinformation.  Appreciate you taking the time to help!

Chris

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2020 Mar 10, 2020

OK. Next question is what should happen if one (or both) of the fields is empty?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2020 Mar 10, 2020

I've made the fields mandatory through field properties (and made them red) so they need to fill them in to submit.  As far as the calculation, I'd like the message to show up if one field isn't populated but that's not key - our biggest data quality concern at the moment is the users are flipping the dates around.  thx!

 

UPDATE: I think I have figured it out.  Needs further testing but so far this is working......

var d1 = this.getField("dec_to_treat_date").value; 
var d2 = this.getField("ref_date").value; 
var message = this.getField("date_message"); 
var dateDecToTreat = util.scand("dd-mmm-yyyy", d1); 
var dateReferral = util.scand("dd-mmm-yyyy", d2); 
var diff = dateDecToTreat.getTime() - dateReferral.getTime(); 
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
if(days <0)
{message.display = display.visible;}
else
{message.display = display.hidden;}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2020 Mar 10, 2020

Well done!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 10, 2020 Mar 10, 2020

Thanks @try67 - you're comment about the date formatting poked my brain and after some additional googling I found the scand stuff.  

One other question if i may - how do i test if a DATE field (ref_date) is not null?  I've tried !== null, != null, != "", !== "", ref_date.value.length >0, etc. and nothing's working.  FWIW, I have debugged and found that the var dateReferral = util.scand("dd-mmm-yyyy", d2);  line gets populated with today's date if ref_date doesn't have anything, but even when I refer back explictly to ref_date it still doesn't seem to recognise that the field is empty

Have a great afternoon and thanks again 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 10, 2020 Mar 10, 2020

Use valueAsString instead of value and then use !="" to check if it's not empty.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 11, 2020 Mar 11, 2020

...now why isn't that information available in any of the books I've looked at or any other documentation??  Grrrr...

If you ever consider writing a book on this please let me know.  I would a) happily review and b) buy multiple copies.  

 

Thanks again so much.  

 

For anyone else needing this sort of functionality, here's the final code I've used.   Basically my Dropdown127 has three options - New Referral, Re-Referral and No Referral.  If New or re-Referral are selected, my three dates are required - ref_date (Referral Date), dec_to_treat_date (Decision to treat) and consult_date (Consult Date).  If No Referral is selected, Referral Date is then optional.  

The code below is then in the Calculate tab for my three dates.  If for New or Re-referrals, Referral Date < Consult Date < Decision to Treat and a message box appears if one of this is not met.  For No Referrals, Referral Date is optional but if populated < Consult Date < Decision to Treat date.  I'm using the Dates both as Value (need them in the calculations) and ValueAsString (need to test for NULL).  

var d1 = this.getField("ref_date").value; 
var d1a = this.getField("ref_date").valueAsString; 
var e9 = this.getField("Dropdown127").valueAsString; 
var d2 = this.getField("consult_date").value; 
var d2a = this.getField("consult_date").valueAsString; 
var d3 = this.getField("dec_to_treat_date").value;
var message = this.getField("date_message"); 

var dateReferral   = util.scand("dd-mmm-yyyy", d1);
var dateConsult    = util.scand("dd-mmm-yyyy", d2);  
var dateDecToTreat = util.scand("dd-mmm-yyyy", d3); 

var diff1 = dateReferral.getTime() - dateConsult.getTime(); 
var diff2 = dateReferral.getTime() - dateDecToTreat.getTime(); 
var diff3 = dateConsult.getTime() - dateDecToTreat.getTime(); 

var oneDay = 24 * 60 * 60 * 1000;
var days1 = Math.floor(diff1/oneDay);
var days2 = Math.floor(diff2/oneDay);
var days3 = Math.floor(diff3/oneDay); 

if (e9 != "No Referral")
{
    if(days1 >0 || days2 >0 || days3>0)
    {message.display = display.visible;}
    else
    {message.display = display.hidden;}
}
else
{
    if((days1 >0 && d1a !="") || (days2 >0 && d2a !="") || days3>0)
    {message.display = display.visible; 
}
else
{
message.display = display.hidden; 
}
}

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 13, 2025 May 13, 2025
LATEST

My brain has gone to the dogs! LMAO! I was thinking decision to treat was whether or not to give a dog a treat. But thank you for posting your full code later in the thread. I found it useful. 🙂

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines