Maxi Bisurgi
Maxi Bisurgi

Reputation: 353

Problems with dates with MongoDB and Mongoose

I am building a react application using and express backend with MongoDb as database. One of my models have a property date, of type Date.

const InfoSchema = new Schema({
    fecha: {
        type: Date,
        required: [true, 'Fecha es un valor requerido']
    },
    caja: {
        type: Number,
        required: [true, 'Caja es un valor requerido']
    },
    bancos: {
        type: Number,
        required: [true, 'Bancos es un valor requerido']
    },
    cheques: {
        type: Number,
        required: [true, 'Cheques es un valor requerido']
    },
    debito: {
        type: DeudaSchema
    },
    credito: {
        type: DeudaSchema
    }
});

Then, on my react app, I have a form where I enter all this info. But if I put a date like 01/11/2017 for example, the saved date is 31/10/2017. For what I have read it is because MongoDd stores dates on UTC format. Is there any way of changing this behaviour? Because also, this problem also give me problems when I try to search by date. The quick way would be to store date as string, but I would like to keep them on date format. Hope someone can help! Thanks very much!

Upvotes: 0

Views: 276

Answers (1)

Prasad Hirlikar
Prasad Hirlikar

Reputation: 21

You have to convert your date to only date and no time value - Date.toISOString() Mongoose should be able to interpret that safely. But this will be timezone independent which may or may not work in your case.

Upvotes: 1

Related Questions