TypeError: Date is not a constructor
So, I've been making forms for my company for some time now with fairly lightweight Javascript that has worked for me in the past. However, he suddenly knocked out the error:TypeError: Date is not a constructor
Code:
var Date = this.getField("Text1");
Date.value = util.printd("mm/dd/yyyy",new Date());
It works on all my old forms, but now it won't work on the new ones ... and I tried to create a new button on the old form - just copy and paste the code and then it will break all other buttons and spit out the same error.
Startup: Windows 7 64-bit with Acrobat XI 11.0.10
source to share
The variable Date
hides the global function Date
and raises this error. Because of how scoping works in JS, the most important use of the name is the most important.
In this case, you declare var Date
which becomes the only one Date
that the function knows about. When you assign a field or text ( Date = this.getField...
) to it, you hide the global class.
You can rename the variable (I would suggest Date
since capital names are usually reserved for types) or explicitly refer to new window.Date
when you go to build a new date.
source to share
I had this problem and I solved it! do not use "Date" as a variable, because it conflicts with the global function Date ();
Example: Wrong!
var Date = new Date();
document.getElementById('dateCopy').innerHTML = Date.getFullYear();
On right:
var DateTime = new Date();
document.getElementById('dateCopy').innerHTML = DateTime.getFullYear();
In your case:
var DateTime = this.getField("Text1");
DateTime.value = util.printd("mm/dd/yyyy",new Date());
source to share