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

How to determine whether a string is a valid date?

fanchunrong
Registered: Mar 4 2008
Posts: 37
Answered

In my form. I break the date field into 3 textboxes. Text_day, Text_Month, and Text_Year.

After user enters all the 3 parts of a date into these 3 boxes, I need to combine them together and determined the combined string is a valid date or not. Is there any function like IsDate(variable) in Acrobat? If not, how can I do it?

Thanks!

My Product Information:
Acrobat Pro 8.0, Windows
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
You can use Acrobat JavaScript's "util.scand()" method to test a date string against a specific format. If valid it will return the date time object for the date and if the date is not a vaid date it will return a null value.

function testDate(sYear, sMonth, sDate) {console.println(sYear + ' ' + sMonth + '  ' + sDate);console.println('new Date(sYear, (sMonth - 1), sDate): ' + (new Date(sYear, (sMonth - 1), sDate)));console.println('util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate): ' + util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate));console.println('(util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate) == null): ' + (util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate) == null) );console.println('');} testDate("2009", "13", "12"); testDate("2009", "12", "13");

And the results are:

Quote:
2009 13 12
new Date(sYear, (sMonth - 1), sDate): Tue Jan 12 2010 00:00:00 GMT-0600 (Central Standard Time)
util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate): null
(util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate) == null): true

2009 12 13
new Date(sYear, (sMonth - 1), sDate): Sun Dec 13 2009 00:00:00 GMT-0600 (Central Standard Time)
util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate): Sun Dec 13 2009 15:44:34 GMT-0600 (Central Standard Time)
(util.scand("yyyy/mm/dd", sYear + "/" + sMonth + "/" + sDate) == null): false


undefined
Note that the 'new Date()' method will adjsut the values to a valid date, while the 'util.scand()' method requires a valid date string for the specified format.

George Kaiser

fanchunrong
Registered: Mar 4 2008
Posts: 37
It works! Thank you!
gkaiseril
Expert
Registered: Feb 23 2006
Posts: 4308
One could also use the following fucntion using the JavaScript date object to achieve the same result.
function isDate(nYear, nMonth, nDate) {/*Validates the given parameters and returns

the date object based on them or null for an invalid date

*/
// establish current date time objectvar oDate = new Date();nYear = Number(nYear);nMonth = Number(nMonth) - 1; // zero based monthsnDate = Number(nDate); // set test date - adjust month value to zero based monthoDate.setFullYear(nYear, nMonth, nDate); // test the parts of the date and return a null if any is invalidif(oDate.getFullYear() != nYear) {return null;}if(oDate.getMonth() != nMonth){return null;}if(oDate.getDate() != nDate) {return null;}// all parts are valid so retrun date time objectreturn oDate;} // end isDate() function

George Kaiser