TypeError: Date is not a constructor
Asked Answered
J

5

19

So, I've been making forms for my company for some time now with pretty easy Javascript that has worked for me in the past. However all of a sudden it's kicking out the error: TypeError: Date is not a constructor

The 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 new ones... and I've tried making a new button on an old form - just copying and pasting the code, and then it'll break all the other buttons and spit out the same error.

Running: Windows 7 64-bit with Acrobat XI 11.0.10

Jarredjarrell answered 8/5, 2015 at 18:10 Comment(0)
C
26

The variable Date is hiding the global function Date and causing this error. Because of how scoping works in JS, the inner-most use of a name is the one that matters.

In this case, you declare var Date which becomes the only Date the function knows about. When you assign it a field or text (Date = this.getField...), you hide the global class.

You can rename your variable (I would suggest date, as capital names are typically reserved for types) or explicitly reference new window.Date when you go to construct a new date.

Croydon answered 8/5, 2015 at 18:14 Comment(2)
Had the same issue using AngularJs. There was a service that was being injected called 'Date'. Renaming the service (if possible) or using window.Date solves the issue.Wheeze
For anyone coming here searching in relation to React or similar; there was a JSX-element named Date to add such a strange behaviour of not finding Date in the execution environment which was web too; The answer hinted to look for in that direction.Medan
K
11

This worked for me:

  var d = new window.Date();
Kulp answered 28/1, 2019 at 12:53 Comment(1)
I was using var x = new Date(someNumber) in React and it wasn't working. This approach solved my problem. Thanks!Driven
S
1

Might be this answer will be helpful in future. I was using below code

var dateTime=new date(); 

But right code is

var dateTime=new Date();
Switchblade answered 31/8, 2021 at 15:15 Comment(0)
P
0

You can't define a variable called "Date" because there's a built-in object in JS called that (you're using it in your code, actually). Change the name to something else.

var Date= somthing; <-- wrong declare, you should not use build -in object name

Partlow answered 7/2, 2017 at 14:24 Comment(1)
There is no problem with defining the variable named Date. The problem is that it is a global variable that might be used by many other things on a page. Check @Croydon answer for detailsDoorjamb
A
0

I was having this problem and I solved it! don't use "Date" as variable because this causes conflict with Global function Date();

Exemple: Wrong !

var Date = new Date();
     document.getElementById('dateCopy').innerHTML = Date.getFullYear();

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());
Amsden answered 17/4, 2018 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.