2

Sorry if this duplicated.

I saw +new Date in a github project then I try it out.

It's return timestamp in type of number.

While new Date() return time format in string.

So what is the meaning of +new Date syntax and how to implement in my own module

kitta
  • 1,723
  • 3
  • 23
  • 33

1 Answers1

3

This is standard javascript. Not node specific

When calling a constructor with new the parenthesis are optional if it takes no arguments

function MyObject () {}

new MyObject();
new MyObject;  // these both create an object

The + is just a shorthand way of casting to a number.

Its the unary plus operator simmilar to the unary minus operator in -5

+'123' === 123 // true

In the case of +new Date this casts a Date object to a number or the current number of milliseconds since the unix epoch. Result is the same as date.getTime().

ANTARA
  • 810
  • 1
  • 13
  • 20
t3dodson
  • 3,949
  • 2
  • 29
  • 40