Home >Database >Mysql Tutorial >Here are a few title options, each playing on different aspects of the problem: **Emphasizing the discrepancy:** * **Why are Timestamps Off When Querying MySQL with NodeJS?** * **NodeJS and MySQL:
NodeJS MySQL Timezone Discrepancy
When querying MySQL directly, the returned timestamps accurately reflect the server's UTC timezone configuration. However, querying MySQL via NodeJS resulted in timestamps adjusted to UTC 2, the local timezone.
Explanation
NodeJS by default employs the system's local timezone for date and time operations. When connecting to a database with a different timezone, such as MySQL's UTC, NodeJS continues to interpret the timestamps according to the local timezone. This leads to the observed timezone discrepancy.
Solution
To resolve this issue and retrieve timestamps in UTC with NodeJS, it is necessary to explicitly set the timezone for the MySQL connection. This can be achieved by adding the following line to the MySQL connection configuration:
timezone: 'utc'
For example, in the provided index.js file:
<code class="js">const db_config = { host: 'localhost', user: 'username', password: 'password', database: 'databaseName', timezone: 'utc' };</code>
The above is the detailed content of Here are a few title options, each playing on different aspects of the problem: **Emphasizing the discrepancy:** * **Why are Timestamps Off When Querying MySQL with NodeJS?** * **NodeJS and MySQL:. For more information, please follow other related articles on the PHP Chinese website!