I am trying to get the age based on two dates: Date of birth and date of visit. Everytime I try to use getFullYear();
in the following manner: I get the error that
var b = getField("dob").value;
var c = b.getFullYear();
I get this error in the console.
b.getFullYear is not a function
2:Console:ExecException in line 2 of function top_level, script Console:Exec
TypeError: b.getFullYear is not a function
2:Console:Exec
undefined
What am I doing wrong? I've tried all sorts of ways to make this work and the only thing that does is I use substring()
to get the last 4 digits of the date.
The date is formatted as mm\dd\yyyy.
Thanks!
I am trying to get the age based on two dates: Date of birth and date of visit. Everytime I try to use getFullYear();
in the following manner: I get the error that
var b = getField("dob").value;
var c = b.getFullYear();
I get this error in the console.
b.getFullYear is not a function
2:Console:ExecException in line 2 of function top_level, script Console:Exec
TypeError: b.getFullYear is not a function
2:Console:Exec
undefined
What am I doing wrong? I've tried all sorts of ways to make this work and the only thing that does is I use substring()
to get the last 4 digits of the date.
The date is formatted as mm\dd\yyyy.
Thanks!
Share Improve this question asked Jun 23, 2014 at 12:58 user3228698user3228698 873 gold badges3 silver badges8 bronze badges 2-
Can you add the contents of
getField
because it looks like you're not returning a date object. – Andy Commented Jun 23, 2014 at 12:59 - the dob field is a text field formatted as dd/mm/yyyy Is that what you were asking? This is my first time coding anything, so please bear with my questions even if they sound inane. Thanks! – user3228698 Commented Jun 23, 2014 at 13:03
3 Answers
Reset to default 1try var b = new Date(getField("dob").value);
You can check for type of b to be of Date For Eg:
var b = getField("dob").value;
if(Object.prototype.toString.call(b) === '[object Date]')
{
var c = b.getFullYear();
}
else{
alert("Not valid date");}
In a ment you said
the dob field is a text field formatted as dd/mm/yyyy Is that what you were asking
...and you've tagged your question acrobat
, so I'm guessing this is JavaScript running within a PDF, not in a web browser.
On browsers, if you try to parse a string in the form dd/mm/yyyy
(say, 03/05/2014
), regardless of the locale of the browser (U.S., UK, Spanish, Russian, Japanese), the browser will try to read it as mm/dd/yyyy
(American format, month-day-year) first. This is not specified anywhere (certainly not in the JavaScript spec), but it's been true of every browser I've ever tried it on.
If Acrobat does the same thing, and you have the text field formatted as dd/mm/yyyy
, you'll run into trouble — is 03/05/2014
May 3rd or March 5th? In the format you're using, it's May 3rd, but in American format it's March 5th.
Unless you want to test Acrobat in multiple locations (or can find a reference promising you how it will parse that string), you'll want to parse it yourself.
If you just want the year at the end, that's fairly simple:
var match = getField("dob").value.match(/(\d{4})\s*$/);
var year = match ? match[1] : undefined;
That'll be a string. If you want to make it a number, you can change the second line slightly:
var year = match ? +match[1] : undefined;
(The +
converts match[1]
, which we know is all digits, to a number.)
If you want the full date, there are several answers here on SO that demonstrate parsing a date with a well-defined format.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744399117a4572296.html
评论列表(0条)