Dealing with timezone mismatches between Node.js and MySQL databases

I am struggling with the timezone settings on my server.

My backend is using Node.js and Express routes for services.

I adjusted the server time to the correct one by running:

dpkg-reconfigure tzdata

I verified that the server time appears accurate.

I am utilizing the "data" function to fetch the current time, which is showing correctly for Saudi Arabia.

The issue arises when a record is created in the database, as it reflects an incorrect time with a delay of 3 hours!

Even though both the database time and server time are correct, why is the record being created with the wrong timestamp?

Answer №1

The database stores time in UTC, which is 3 hours behind Saudi Arabia time. To accurately display the correct time in Saudi Arabia, maintain the database timezone as UTC and adjust retrieved records by adding 3 hours for conversion to local time.
For time-based searches, subtract 3 hours from the desired time to convert it to UTC time.

Answer №2

I recall that this solution resolved an issue related to time zones.

var timeZoneDifference=(this.date.getTimezoneOffset()-dateFuture.getTimezoneOffset())/60;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP: Retrieving the current time zone from the database

In my database, I save dates in DATETIME format as Y-m-d H:i:s. My goal is to display the date from the database in the user's local time. For instance, if the user is located in France, I want to show their timezone. Is there a way to accomplish thi ...

Guide on using webpack to import jQuery

When using webpack, is it necessary to install the jquery file from npm, or can I simply require the downloaded file from the jquery website? directory app/ ./assets/javascripts/article/create/base.js ./assets/javascripts/lib/jquery-1.11.1.min.js webpack ...

Executing code following the installation of an NPM module

Can a post-processing script be executed automatically after installing an NPM module? In my node.js project, I encounter situations where I need to install specific NPM modules. Upon installation of these modules, there is a requirement to automatically ...

Calculate age in years and months using a PHP timestamp

Currently, I am executing the following SQL script: SELECT TIMESTAMPDIFF(YEAR,CONCAT(dob_year, '-', dob_month, '-', dob_day),CURDATE()) as age FROM members This query is used to calculate the ages of users based on a timestam ...

Determining the optimal scenarios for utilizing debug compared to alternative logging frameworks

NodeJS offers two well-known logging frameworks: winston and Bunyan. Additionally, there is a tool called debug. From my understanding, all of these tools essentially serve the same purpose - which is to log information. debug comes as a default component ...

Troubleshooting Node.js error: Connection Refused while attempting to install packages

Currently, I am following the learnyounode tutorial to enhance my understanding of node.js. However, I keep encountering an error whenever I attempt to install a new package. npm ERR! Linux 4.2.0-c9 npm ERR! argv "/home/ubuntu/.nvm/versions/node/v4.1.1/bi ...

What is the best way for me to trigger the error handler by using the "next()" function in this project?

Currently, I am conducting a test scenario that involves running a POST request to a specific endpoint using malformed and edge cases. test("POST /register, malformed and edge cases", async () => { await request(server).post(`/auth ...

Guide to establishing a connection between Reactivesearch and an external Elasticsearch cluster

Trying to link my reactive search app with an external Elasticsearch provider (not AWS) has been a challenge. The provider does not allow any modifications to the Elasticsearch cluster or the use of Nginx in front of it. In accordance with the reactive se ...

The asynchronous/await methods within a loop for connecting to multiple servers through ssh2 are not functioning as expected

I'm working on a tool that interacts with multiple servers using the npm library ssh2. My goal is to automatically make changes to a specific file on all these servers by looping through them. Below is a snippet of the code I have been working on: co ...

Incorporate a PHP variable named "result" into a MySQL query

Is it feasible to integrate PHP data into a MySQL result? Let me elaborate: Consider two tables, one containing user actions and the other user information. By querying the actions table, I retrieve user IDs and count each one grouped by the user: $ids = ...

Encountered a setback while constructing a library of Angular 7 components

While I successfully created a component library in Angular version 11 without any issues, I encountered an error when trying to build a component library in Angular version 7. Cannot find module 'tsickle/src/tsickle' Require stack: - D:\Ang ...

Does npm-check-updates have the ability to lock specific dependencies during the execution of ncu -ua command?

Utilizing npm-check-updates, we are managing updates for our dependencies listed in the package.json. We are encountering challenges due to having numerous small projects that require fixed versions of specific dependencies. As a module writer, we prefer ...

unable to connect a pipe after the response data has been emitted in the npm request

I am having an issue with using the npm request package. I am unable to pipe after the response is emitted in my code snippet below var fs = require('fs'), request = require('request'); var readStream = request({ url: 'https: ...

What is the best way to distribute shared client-side javascript code among NodeJS projects?

As a newcomer to Node, I am embarking on a journey with a few web app projects using Express. Across these projects, there are some client-side libraries that I would like to share. It seems like a common issue with many possible solutions already in exist ...

Unacceptable POST Request Inputs

I'm encountering an issue with invalid inputs while making a signup POST request in Postman. I have reviewed my User Model attributes but I am unable to identify which input(s) are causing the error. Below you will find details of my model, controller ...

Prevent EACCES issues with npm/node when using Ubuntu?

Setting up the system environment $ lsb_release -a 2> /dev/null | grep Desc Description: Ubuntu 14.04.1 LTS $ sudo apt-get install build-essential $ sudo add-apt-repository ppa:chris-lea/node.js && sudo apt-get update $ sudo apt-get install ...

Tips on how to patiently wait until the database connection is established and all queries are successfully executed for every database specified in an array

A file containing JSON data with database details needs to execute a series of queries for each database connection. The map function is currently waiting for the database connection. Below is the start function function start() { console.log('func ...

Creating a custom output using PHP, MySQL, and JSON

When I retrieve and print data from a MySQL database using the json method, it looks like this: $ret = array(); $fileName = Access::FetchDB("SELECT name FROM tags"); $ret[] = $fileName; echo json_encode($ret); The current output is: [[{"name":"test1"},{ ...

Encountered a problem when trying to install socket.io on a Ubuntu virtual machine running on a Windows

During my attempt to install socket.io with the command npm install socket.io, I encountered the following error: npm ERR! Error: ENOENT, chmod '<path_to_project>/node_modules/socket.io/node_modules/socket.io-client/node_modules/engine.io-clien ...

Skipping a query in a column's WHERE statement when the value is null can be accomplished by using

I have a simple table set up like this: const Sequelize = require('sequelize'); const sequelize = require('../../util/database'); const Speed = sequelize.define('speed', { id: {type: Sequelize.INTEGER, autoIncrement: true ...