Home  >  Article  >  Database  >  How to Fix Timezone Discrepancies When Fetching MySQL Data with NodeJS?

How to Fix Timezone Discrepancies When Fetching MySQL Data with NodeJS?

Susan Sarandon
Susan SarandonOriginal
2024-10-25 08:19:02351browse

How to Fix Timezone Discrepancies When Fetching MySQL Data with NodeJS?

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!

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