Home > Database > Mysql Tutorial > body text

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
Release: 2024-10-25 04:08:29
Original
936 people have browsed it

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'
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!