1

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

b.ben
  • 1,368
  • 3
  • 17
  • 28

1 Answers1

2

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
  • 760
  • 1
  • 12
  • 20
t3dodson
  • 3,734
  • 2
  • 31
  • 38