Troubleshooting MySQL Timezone Difference When Fetching Data with NodeJS
When retrieving data from MySQL directly, many users encounter a discrepancy between the expected UTC timestamps and the actual UTC 2 timestamps returned. This discrepancy stems from a default timezone setting in NodeJS that differs from the timezone set on the MySQL server.
To rectify this issue, it's essential to explicitly set the timezone for the NodeJS MySQL connection. One way to achieve this is by adding the 'timezone': 'utc' option when initializing the MySQL connection:
var db_config = { host: 'localhost', user: 'xxx', password: '', database: 'xxx', timezone: 'utc' // This line was missing };
By specifying 'utc' as the timezone, NodeJS will force the returned timestamps to be in UTC, aligning them with the timestamps obtained from direct MySQL requests.
The above is the detailed content of How to Fix Timezone Discrepancies When Fetching MySQL Data with NodeJS?. For more information, please follow other related articles on the PHP Chinese website!