Search code examples
mongodbmomentjsisodate

How to convert mongodb $date.$numberLong result to a moment.js using JavaScript?


I'm getting the $date.$numberLong from a mongodb collection to handle in javascript. The JSON is like this:

[{
    "_id":{"$oid":"5b6ad63479ff904a550957eb"},
    "date":{"$date":{"$numberLong":"1533686400000"}}
}]

If I print the value.$date.$numberLong it returns 1533686400000 as well. But, I've been trying by this way:

var date = moment(value.$date.$numberLong);

and it has returning invalid date. How can I get it work?


Solution

  • Following the Anthony contribution:

    moment(parseInt(value.$date.$numberLong));
    

    it has working now!