I don't know if the title is appropriate because more than an error I have a strange behavior in my NodeJs application with express and Postgresql
To begin with, I insert my DB into a field called date of the timestamp type in the following way:
var newDate = new Date();
// agrego 5 minutos a la fecha y hora actuales
newDate.setMinutes(newDate.getMinutes() + 5);
// convierto a string el resultado
var string_newDate = newDate.toISOString().replace(/T/, ' ').replace(/\..+/, '')
console.log('tipo:', typeof(string_newDate), ', valor:',string_newDate);
//: tipo: string valor: 2020-12-14 20:17:53
// guardo en la DB:
database.query('INSERT INTO usuario(fecha) VALUES ($1)', [string_newDate])
When doing a query to the DB by terminal it shows me exactly that value:
Select fecha from usuario where id = 1;
// obtengo: 2020-12-14 20:17:53
The problem arises when I make the query from my nodejs application:
const result = await database.query('SELECT fecha from usuario where id = 1');
...
console.log('tipo:', typeof(result.rows.fecha), 'valor:', typeof(result.rows.fecha))
// obtengo esta fecha:
// tipo: object valor: 2020-12-15T01:17:53.000Z
The date I get when doing the query with nodejs is 2020-12-15T01:17:53.000Z not what I have stored in my database which is 2020-12-14 20:17:53 .
As additional data, no matter what date I save, the date I get when doing the query with nodejs is 5 hours above the one I saved , as in this case.
I appreciate any help with this problem, I'll be here as long as it takes.