NodeJS MySQL Timezone Discrepancy
When working with MySQL, users may encounter a difference in timezone when fetching data directly from the MySQL server compared to using NodeJS. By default, NodeJS assumes the local timezone. To resolve this issue and retrieve UTC timestamps, it's necessary to explicitly set the timezone when establishing the MySQL connection.
In the provided example, the user faced the issue of MySQL server timestamps being displayed in UTC when retrieved directly, while NodeJS was returning timestamps in the local time zone (UTC 2). This discrepancy was due to the lack of timezone specification in the NodeJS MySQL connection.
Solution:
To address this, the user added a timezone parameter to the MySQL connection configuration in their NodeJS code.
<code class="javascript">var db_config = { host: 'localhost', user: 'xxx', password: '', database: 'xxx', timezone: 'utc' };</code>
By explicitly setting the timezone to 'utc' in the connection configuration, NodeJS will retrieve timestamps in Coordinated Universal Time (UTC) when making database queries. Ensuring proper timezone configuration allows for consistent handling of timestamps across different platforms and avoids timezone-related errors.
The above is the detailed content of Here are a few title options, keeping in mind the question format and the content of your article: **Option 1 (Direct and concise):** * **Why are MySQL timestamps different in NodeJS?** **Option 2. For more information, please follow other related articles on the PHP Chinese website!