TypeError: Date is not a constructor

白昼怎懂夜的黑 提交于 2019-12-01 16:22:48

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.

This worked for me:

  var d = new window.Date();

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

Paulo Victor Ferreira

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());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!