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

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

Susan Sarandon
Release: 2024-10-25 08:19:02
Original
462 people have browsed it

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

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!

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