I have a VUE application that will be used in multiple times zones. Data is sent to a nodejs express restful service and stored in a mysql database.
My approach will be to store everything in the database in UTC and when I need to display the date/time in the UI convert it locally. (Feel free to offer a smarter way to do things).
If I set a variable in my UI to now() and save that variable to the db I see everything shifted 5 hours. I'm in new york with a 5 hour offset so that feels correct.
If I hit this service call that I made for testing things work as expected.
app.get("/datetest", (req, res) => {
//create a hardcoded date object for testing
const date = new Date("2020-01-01T00:00:00.000Z");
return res.json({
date: date
});
});
I see the date in the browser with no adjustments like I would expect.
{"date":"2020-01-01T00:00:00.000Z"}
It seems that my issues are with retrieval from the database. I created a simple test case that gets the current time from the server and it has me super confused.
My Data access layer looks like this:
function selectNowFromTheDB(callback) {
const query = "SELECT NOW() as now";
db.query(query, (err, results) => {
if (err) {
logger.error(query, err);
callback(err, null);
} else {
callback(null, results);
}
});
}
and I call it using this code:
app.get("/datetest2", (req, res) => {
dataAccess.selectNowFromTheDB( (err, data) => {
if (err) {
res.status(500).json({
error: "Error retrieving now from the database",
});
} else {
console.dir (data);
res.json(data);
}
});
});
This function returns 5 hours later that I would expect.
I'm missing something. Most of the help that I have found suggest that I set the db timezone to my timezone but that feels wrong to me since other (potentially concurrent) users will be using it in other places.
Thanks in advance for any help that you might have.