Raphael Deiana
Raphael Deiana

Reputation: 750

Get a correctly formatted date from mysql

I have a problem getting a datetime from mysql in a nodejs app. The returned date isn't in the right format.

I have this row in my table :

id: 1
event: xxx
date: 2020-01-31 15:00:00

I get the row this way:

let sql = "SELECT * FROM events";
db.query(sql, function(error, results) {
  console.log(results);
  ...
}

And the returned row is :

RowDataPacket {
    id: 1,
    event: 'xxx',
    date: 2020-01-31T14:00:00.000Z
}

If I push this row to another table (consumed_events), the inserted date in the table is:

2020-01-31 14:00:00

instead of

2020-01-31 15:00:00

What can I do to prevent this behaviour ? I don't know how I could get the right date from the database.

Thank you in advance !

Upvotes: 1

Views: 126

Answers (1)

Raphael Deiana
Raphael Deiana

Reputation: 750

I got it working thanks to nbk's comment.

The solution was to add the timezone when starting the mysql connection :

db = mysql.createConnection({
    host:     "...",
    user:     "...",
    password: "...",
    database: "...",
    timezone: 'utc'
});

Upvotes: 1

Related Questions