2

In a function I have the line:

const currentTime = new Date.getTime();

The code compiles just fine. Also, ESLint displays neither warnings or errors.
But when I run the code, I get an alert box giving the confusing message Date.getTime is not a constructor.

What is wrong here?

Error message: 'Date.getTime is not a constructor'


I have noticed that there is no error if I don't call the function that contains the line above.

The following Stack Snippet reproduces the error.

function rightNow() {
  const currentTime = new Date.getTime();
  return currentTime;
}
console.log(rightNow().toString());
// ^^ No error if I comment out the previous line! ^^
.as-console-wrapper { max-height: 100% !important; top: 0; }
Henke
  • 4,445
  • 3
  • 31
  • 44
  • It's `(new Date).getTime()` or `new Date().getTime()` - or just use `Date.now()`. What you've written is `new (Date.getTime)`. – Bergi Apr 04 '22 at 13:12
  • The compiler doesn't throw an error because it's grammatically correct: the *new* keyword followed by a function call is fine. However, not all functions are constructors. BTW, `new Date().getTime()` is functionally identical to `Date.now()`, which is less to type. :-) – RobG Apr 04 '22 at 13:13

1 Answers1

4

1. Quick fix

Just add parentheses immediately after new Date :

const currentTime = new Date().getTime();

2. Explanation

Date.getTime is not a constructor

If the compiler were a thinking human being, it could respond to the snippet

new Date.getTime();

by saying Aha! I think I get what you mean: there is a class named Date.getTime and now you want to call the default constructor to create a new object: new Date.getTime(), right?
Well, sorry, but I could not find any such constructor.

And I can hear you – the programmer – screaming No!! That's not what I meant! (Stupid ✴!☠#@☠$% compiler!)

After you have chilled down enough to restore a reasonably normal blood pressure, you might – patiently – try something like:

Mr Compiler, we obviously got off on the wrong foot – I never meant to hurt your feelings. What I want is: please create a new Date object, and then call the getTime() method on that newly created object.
Look, let me put it as follows, to see if you better understand my point of view?

const currentTime = (new Date).getTime();

Missing '()' invoking a constructor new-parens

To invoke the default constructor, '()' is missing.

The response by Mr Compiler :

Missing '()' invoking a constructor  new-parens

Or – in a more human langage.
OK! So you want to create a new Date object? And on that object you want to .getTime()? That's fine. – Except that you forgot to tell me which constructor you want me to use. Is it the default constructor? Or some other constructor?
Since you didn't tell me, I will assume you want me to use the default constructor.

But would you consider being more explicit the next time you ask me to do something like this?
That way I would not have to make an educated guess on what you mean.

After resigning with a sigh, you – the programmer – try again:

const currentTime = (new Date()).getTime();

Finally – no complaints from Mr Compiler!

This time around, Mr Compiler responds with silence, which is good news! – Responding with silence shows that Mr Compiler is in his very best mood.

Still, you are a human and not a compiler, so you could imagine Mr Compiler saying:
OK. So (new Date()).getTime() means you want to create a new Date object using the default constructor, and then call getTime() on that object. It's all fine. I have no complaints.

But since you are a very sensitive human, you cannot help yourself asking:

Good! I'm glad we finally agree on this. But is there something still on your mind? Forgive me for asking.

Upon which you imagine Mr Compiler responding:

No no. Your code makes sense. It's OK. It really is. But well, since you keep nagging me – writing (new Date()) is fine. There is nothing wrong with it. But you don't need the surrounding parentheses. Once you write new Date() then I understand that you want to create a Date object, even if you then call a method on that object.

I am not stupid you know!

Epilogue

As a general rule, runtime errors – like in this question – are often much harder to understand and debug than compiler errors and warnings. This is when a Stack Overflow question-and-answer might be helpful. As pointed out in the comments, a shorter alternative to new Date().getTime() is the plain and simple Date.now().

Here is a Stack Snippet without the error.

function rightNow() {
  const currentTime = Date.now();
  return currentTime;
}
console.log(rightNow().toString());
.as-console-wrapper { max-height: 100% !important; top: 0; }

Reference

Henke
  • 4,445
  • 3
  • 31
  • 44
  • 1
    nicely explained – Gabriele Petrioli Apr 04 '22 at 13:01
  • "*The response by Mr Compiler: `Missing '()' invoking a constructor new-parens`*" - that's not Mr Compiler, that's Mr Linter trying to interrupt your conversation with the compiler. – Bergi Apr 04 '22 at 13:15
  • _that's Mr Linter trying to interrupt your conversation with the compiler_ Thanks! You are right of course. Still, I might not change my answer accordingly, as I don't want to complicate the story more than necessary ... Or maybe I will ... Thanks anyway. – Henke Apr 04 '22 at 13:23