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

+5


source to share


4 answers


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.

+12


source


You cannot define a variable named "Date" because the built-in object in JS named it (you are using it in your code, actually). Change the name to something else.



var Date = somthing; <- wrong declare, you shouldn't use build -in object name

0


source


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());

      

0


source


This worked for me:

  var d = new window.Date();

      

0


source







All Articles