1

Is there a way to get the complete timestamp(including date, hour of the day, min, microseconds and timezone) in Node.js?

I am using node version 4.3.0.

Oscar LT
  • 787
  • 1
  • 4
  • 24
isha
  • 21
  • 1
  • 1
  • 3

1 Answers1

6

The easiest way is probably just new Date();

You might also try Date.now() which will give you the current timestamp in milliseconds.

Patrick Grimard
  • 7,033
  • 8
  • 51
  • 68
  • on the node REPL, you will get a properly formatted date if you use `new Date()` whereas `Date.now()` gives a numeric value, which -i'm assuming- is miliseconds since epoch or similar – trve.fahad Mar 08 '16 at 20:22
  • Yes, you're right. I'm just giving both options ;) – Patrick Grimard Mar 08 '16 at 20:29
  • @Zeus77— *new Date()* returns a Date object. Whether the default string representation is "proper" is unknown since the OP hasn't said what that is. – RobG Mar 08 '16 at 21:04
  • @RobG yes, reason I made a *comment* rather than a complete answer :) – trve.fahad Mar 08 '16 at 21:23
  • @PatrickGrimard --- I got the answer using, var a = new Date(); var startTime = a.toJSON().replace(/T/, ' ').replace(/\..+/, ''); It gives the output in the format, 2016-03-07 11:33:48 – isha Mar 10 '16 at 02:45