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

why the current month is not correct?

fanchunrong
Registered: Mar 4 2008
Posts: 37

I have followin code:

var d=new Date();
var curYear=d.getFullYear();
var curMon=d.getMonth();
var curDate=d.getDate();
app.alert ("cYear" + curYear + "cMonth" + curMon + "cDate" + curDate);

What I got on the message window is:
cYear2009cMonth3cDate27.

But today is 2009/4/27. I am suppose to get
cYear2009cMonth4cDate27.

Why the month is not correct?

Thanks!

My Product Information:
Acrobat Pro 8.0, Windows
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi fanhunrong,

That's because in Acrobat JavaScript the months start at zero. This is explained the series of tutorials on Dates and Times in the Learning Center here at AUC-

http://www.acrobatusers.com/tutorials/2006/date_time_part1


Hope this helps,

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com
fanchunrong
Registered: Mar 4 2008
Posts: 37
Does that mean that I need to use d.getMonth()+1 to get the current month?
Thanks!
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi fanchunrong,

No, replace what you have with this-

(d.getMonth()+1).toString()

Hope this helps,

Dimitri
WindJack Solutions
www.windjack.com
www.pdfscripting.com
fanchunrong
Registered: Mar 4 2008
Posts: 37
Dimitri,
Thank you so much for your help! I am new to the acobat Javascript coding. Bear with me if I ask stupid questions.

Would you please tell me how to compare two dates?

Now on my form, I have a Date of Birth field that has been broken to 3 parts (in 3 different textbox).
Textbox1: MM
Textbox2: DD
textbox 3: YYYY

What I want to do is to validate following:

1. combine the value in three boxes and determine whether it is a valid date or not.
2. if it is valid date, make sure it is not a future date.

I can do the first part by using util.scand.

But I don't know how to compare the DOB to current date. Would you please help?

Thank you very much!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4307
You need to convert all the dates to a JavaScript date time object and then convert that date time object to a value using JavaScirpt's 'valueOf()' method. More information about using date time strings is in the Acrobat JS Guide and [url=https://developer.mozilla.org/En/JavaScript]JavaScirpt 1.5 documentation[/url].

var oDate = new Date(); // get today's date time objectconsole.println("Today's date time object is: " + oDate); var curYear = oDate.getFullYear(); // get 4 digit yearvar curMon = oDate.getMonth(); // get zero based monthvar curDate = oDate.getDate(); // get date of monthconsole.println("Today is: \ncYear: " + curYear + " cMonth: " + (curMon + 1) + " cDate: " + curDate, 3, 0); var sTestDate =  "03/26/2008"console.println("\nTest date: " + sTestDate); var oTestDate = util.scand("mm/dd/yyyy", sTestDate);console.println("Test date's date time object: " + oTestDate); var fDate = oDate.valueOf(); // date time object as a numberconsole.println("value of today: " + fDate); var fTestDate = oTestDate.valueOf(); // date time object as a numberconsole.println("value of test date: " + fTestDate); console.println("fDate < fTestDate: " + (fDate < fTestDate) );console.println("fDate == fTestDate: " + (fDate == fTestDate) );console.println("fDate > fTestDate: " + (fDate > fTestDate) );  sTestDate =  "04/28/2009"console.println("\nTest date: " + sTestDate); var oTestDate = util.scand("mm/dd/yyyy", sTestDate);console.println("Test date's date time object: " + oTestDate); var fTestDate = oTestDate.valueOf(); // date time object as a numberconsole.println("value of test date: " + fTestDate); console.println("fDate < fTestDate: " + (fDate < fTestDate) );console.println("fDate == fTestDate: " + (fDate == fTestDate) );console.println("fDate > fTestDate: " + (fDate > fTestDate) );

Which should produce the following on the JS Debugging console:

Quote:
Today's date time object is: Mon Apr 27 2009 15:16:44 GMT-0500 (Central Daylight Time)
Today is:
cYear: 2009 cMonth: 4 cDate: 27

Test date: 03/26/2008
Test date's date time object: Wed Mar 26 2008 15:16:44 GMT-0500 (Central Daylight Time)
value of today: 1240863404355
value of test date: 1206562604402
fDate < fTestDate: false
fDate == fTestDate: false
fDate > fTestDate: trueTest date: 04/28/2009
Test date's date time object: Tue Apr 28 2009 15:16:44 GMT-0500 (Central Daylight Time)
value of test date: 1240949804449
fDate < fTestDate: true
fDate == fTestDate: false
fDate > fTestDate: falsetrue

George Kaiser