Devin Dixon
Devin Dixon

Reputation: 12403

Convert MongoDate using moment.js

I am pulling information from a Mongo database using JavaScript and I get the data in the following json format:

end_date : {
sec: 1453532400,
usec: 0,
__proto__: Object
}

The problem I am running into is trying to convert that date to be a JavaScript DATE_RFC2822 or ISO format. I am using moment.js like this with no results:

moment(response.end_date).toISOString())

It always returns the current date and time. My question is how should I be converting MongoDates in JavaScript to DATE_RFC2822 or ISO formats?

Upvotes: 0

Views: 890

Answers (1)

Emil Vikström
Emil Vikström

Reputation: 91963

The sec property is a Unix timestamp. You can send in that property only:

moment(response.end_date.sec, "X").toISOString()

or alternatively:

moment.unix(response.end_date.sec).toISOString()

Upvotes: 2

Related Questions