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
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