Skip to main content
Participating Frequently
June 26, 2018
Question

Comparing the date entered to ensure it is between today and 4 weeks from now

  • June 26, 2018
  • 2 replies
  • 1963 views

Good morning

I've tried a number of different approaches but can't seem to get this to work.  I'm building a PDF Form in Adobe Pro DC to be used by our staff.  I have an Appointment Date field (appt_date) and I need to verify that the date the user entered is between today and 4 weeks from now.  I've seen a lot of code samples but the dates are always provided in the code, which doesn't help me.

Any help would be appreciated.  This code snippet is the last one I've tried, but it's not working (I think this was a "pure javascript" example rather than "Adobe JavaScript").  Thanks in advance!

Chris

var dateFrom = "01/08/2017";
var dateTo = "01/10/2017";
var dateCheck = "05/09/2017";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateCheck.split("/");

var from = new Date(d1);  // -1 because months are from 0 to 11
var to   = new Date(d2);
var check = new Date(c);

alert
(check > from && check < to);

This topic has been closed for replies.

2 replies

try67
Community Expert
Community Expert
June 28, 2018

For future questions about scripts in Acrobat you'll be better off using this forum: JavaScript

sinious
Legend
June 27, 2018

JavaScript is the same in either instance, but what you can do with it and what contains it may allow it to do more or less. Acrobat is an example of an environment where JavaScript has extended capabilities to work with the interface.

Here is a verbose, really simple way to determine the number of days between 2 dates, down to the microsecond. On the JSFiddle example I just changed how I am displaying the result for that site.

var one_day_in_milliseconds = 1000*60*60*24; // 86400000ms

// date you want to compare

var compare = new Date("09/01/2018")

// todays date (per local machine)

var today = new Date();

// use Date.getTime() to get millisecond comparison difference

var difference = compare.getTime() - today.getTime();

// what to display

var message;

if ( difference > 0 ) {

    // difference in days, ignoring partial days (no decimal)

    var days = Math.floor( difference / one_day_in_milliseconds );

    message = days + " day(s) is";

    // 28 days is 4 weeks

    if ( days <= 28 ) {

          message += " within 4 weeks from now.";

    } else {

          message += " further than 4 weeks from now.";

    }

} else {

    //the compared date is in the past, automatically this isn't desirable

    message = "The date being compared is not valid, it is in the past.";

}

alert( message );

JSFiddle example

There are quite a few ways to do this and this is only verbose to make it very clear what is going on. Searching around for JavaScript date comparison will yield quite a bounty of different ways to do it. This is also ES5 for compatibility.

Note: If you wanted to include partial days, change line 18 from "Math.floor" to "Math.ceil"