2

i'm bulding a simple nodejs rest service for a table in a mysql database, to do so i make server.js has these lines:

app.get('/Attivita',(req,res)=>{
  res.status(200);
   con.query('Select * from attivita', function (err, result) {
    if (err) 
    throw err;

  res.setHeader('Access-Control-Allow-Origin', 'http://isaplomb.org')
  res.send(result)
  })

});

it would return a json with the result of the query, it's correct, it works but in the table Attività there is a column called data that store the date in format date. node js returns a datetime value and it change the value of the date:

data stored in db: 2018-02-24 data returned from nodejs service: 2018-02-23T23:00:00.000Z

i need to show this date in an html page.

can anyone help me?

2 Answers 2

3

You can fix this with dateStrings: true in your connection configuration.

Example:

var connection = mysql.createConnection({
  host: DB_HOST,
  database: DB_DATABASE,
  user: DB_USERNAME,
  password: DB_PASSWORD,
  dateStrings: true
});
Sign up to request clarification or add additional context in comments.

Comments

0

Looking at your questions it seems like TimeZone(TZ) causing date change. Your server and application are in different timezones. You can store your date as UTC and convert at client side depending on client TZ or if you want one specific TZ then convert UTC value to that TZ.

Server does not store TZ value in the data row, the only information we have at the time we receive row is "2018-02-24"

Also the client does not know which TZ server is running. Pass TZ to a client, I think this is what node-mysql is doing.

1 Comment

i simply need to get the data stored in the database no corvesion needed, ho can i do? if node return that date on http request i need to forcing node to respond on his time zone that is the one i need

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.