These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

Date Comparison - Date on or before Alert

clibor
Registered: Aug 30 2010
Posts: 17
Answered

This message is for gkaiseril or for anyone else who can answer this for me.

I found a code that gkaiseril had posted in another forum about 4 years ago. It's a basic code that gives me an alert if a date entered is before the current date and asks you to enter a date on or after the current date.

Problem: If you enter the current date I still get the alert. A date after the current date it's fine.

I need to have so the current date AND on can be entered without the alert.

Any help would be appreciated.

Here's the code:

if (event.value != "")
{
// get system date object
sysDate = new Date();

// get entered date object
tstDate = util.scand("mmmm d, yyyy", event.value);

// compare dates
if (tstDate < sysDate)
{
app.alert("Entered date before current date.\rPlease reenter a date on or after " + util.printd("mmmm d, yyyy", sysDate), 1, 0);
this.getField(event.target.name).setFocus();
event.rc = false;
}
}

My Product Information:
Acrobat Pro 9.3.1, Macintosh
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
You can't compare Date objects like that. You need to convert them to numbers, using the getTime() method. Like so:
if (tstDate.getTime() < sysDate.getTime())

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

clibor
Registered: Aug 30 2010
Posts: 17
Still getting the same result...

Everything is working fine except for the fact that I can't enter today's date w/o the alert popping up.
If today's date wasn't an issue then I'd just change the alert to state that.

If I'm correct in assuming then the line in question is stating that if the date is BEFORE the sys.date then the alert should pop up. Anything ON or AFTER...No alert.

I'm just stumped on this one....
try67
Online
Expert
Registered: Oct 30 2008
Posts: 2399
This comparison is not by days but by milliseconds. If you want to compare by full days, you'll need other methods. Have a look here: http://w3schools.com/jsref/jsref_obj_date.asp

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

George_Johnson
Expert
Registered: Jul 6 2008
Posts: 1876
The problem is you're not just comparing dates here, you're comparing the date and the time. You can adjust the time of either date variable so the comparison behaves as you want. For example:

...
sysDate = new Date();
sysDate.setHours(0,0,0);
...
clibor
Registered: Aug 30 2010
Posts: 17
Thanks!

That worked perfect!!

Thanks for the help guys!!

CLIFF