Mongodb and dates

-1

I have a problem, when I save a date, it is saved like this:

2018-12-04T00: 00: 00.000Z

and I would like the time to be 23:00 not 00:00

2018-12-04T23: 00: 00.000Z

It's an api, with express and node

function SaveOfferDay(req, res){
var update = req.body;
var id = req.params.id;



Offer.update(   {"_id": id},{"$addToSet": {"date": {"day" : new Date("2018-12-04"),}}}, (err, offerUpdated) => {
if(err) return res.status(500).send({message: 'Error en la petición'});
return res.status(200).send({offer: offerUpdated});

});
}
    
asked by my crazy beauty 04.12.2018 в 10:21
source

1 answer

0

Understanding that all the queries that will be made will have an entry date format 'YYYY-MM-DD', then what is intended is to add the time to '23: 00: 00 ', right? In this case, to solve your problem I invite you to take a look at the module of npm momentjs where you can work with the dates of comfortable and simple way.

For your case, I'll show you an example:

const moment = require('moment')

let test = "2018-12-04"

if(moment(test,'YYYY-MM-DD', true).isValid()){

   let fecha = moment(test).hour(23 + 1).minute(00).second(00).millisecond(000)

   console.log(test.toDate()) // => 2018-12-04T23:00:00.000Z

   Offer.update({ "_id": id }, { "$addToSet": { "date": { "day": test.toDate() } } })

} else {
console.log("Formato de fecha errónea (YYYY-MM-DD)")
}

I hope it's your help.

Greetings

    
answered by 07.12.2018 в 19:44