1

When I create objects in my MongoDB using Mongoose, the dates are formatted as, for instance, 2016-10-10T15:35:52.764Z (it might be yyyy-MM-ddTHH:mm:ssZ). If I use Date.now() in JavaScript, I get a timestamp.

How can I create the same date format as Mongoose with javascript?

Edit

It seems I can use

new Date().toISOString();

but what is the difference of, for instance, Date.now() and new Date()?

Are there any reasons for not using new Date() instead of a function under the Date object (that somehow already seems to be initialized without writing new Date()?)

Jamgreen
  • 10,329
  • 29
  • 113
  • 224
  • Possible duplicate of [Performance - Date.now() vs Date.getTime()](http://stackoverflow.com/questions/12517359/performance-date-now-vs-date-gettime) – Arif Khan Oct 11 '16 at 06:26
  • [*Date.now*](http://ecma-international.org/ecma-262/7.0/index.html#sec-date.now) returns a number, [*new Date*](http://ecma-international.org/ecma-262/7.0/index.html#sec-date-constructor-date) returns a Date object whose internal time value is exactly the same as if you'd called *Date.now*. – RobG Oct 11 '16 at 07:06

1 Answers1

0

you can use moment.js and run like this

moment().format('YYYY-MM-DDTHH:mm:ss'); // print 2016-10-11T15:25:36
Alongkorn
  • 3,968
  • 1
  • 24
  • 41