So we had the Discussion today in our company about +new Date()
being good practice or not. Some prefer this way over new Date().getTime()
.
In my opinion, this is pretty convenient but on the other side one would say it's harder to read.
Are there any pros or cons besides the obvious "It's harder to understand for people not familiar with the unary operator"?
Use new Date() to generate a new Date object containing the current date and time. This will give you today's date in the format of mm/dd/yyyy.
log("Now: " + (new Date()). toString()); It's a bit confusing because this piece of code look like a synchronous code: no callback appears in the code. But in fact, it is asynchronous.
Date() returns an implementation dependent string representing the current date and time. new Date() returns a Date object that represents the current date and time. ;-) Yeah, I know.
The Date object is created by using new keyword, i.e. new Date(). The Date object can be used date and time in terms of millisecond precision within 100 million days before or after 1/1/1970.
The getTime
method appears to be a huge amount faster:
Why is this the case?
Here's what happens when you call the getTime
method on a Date
instance:
[[PrimitiveValue]]
internal property of this Date object.Here's what happens when you apply the unary plus operator to a Date
instance:
Date
instance in question[[DefaultValue]]
methodIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With