Converting a mongo stored date back into milliseconds since Unix epoch when loaded?

前端 未结 5 408
名媛妹妹
名媛妹妹 2020-12-15 21:09

I am using Mongoose & Node.js for my webserver.

As a part of one of my document schemas, I have a \'timestamp\' field. The line for it in the schema is:

5条回答
  •  温柔的废话
    2020-12-15 21:36

    As a best practice, I would say: keep your data the type it deserves.

    Anyway, if your client needs to treat with numbers, you can simply pass the date as milliseconds to the client, and still work with Date objects in Node.

    Just call timestamp.getTime() and ta-da, you have your unix timestamp ready for the client.

提交回复
热议问题