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:

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:

Barbara Streisand
Barbara StreisandOriginal
2024-10-25 04:08:291022browse

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: Why is My Data Timezone Different?**

**Focusing on the

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn