In NodeJS / Sequelize / MySQL, how do I insert the time in the local timezone on the server?

We are trying to insert datetime values ​​with the following query - insert into values ​​(name, createdon) ('product1', now ()) We run a raw query using sequelize. The value is stored in UTC format.

By executing the same query directly from MySQL, it stores the value in local time.

We want the value stored in local time, not UTC. How do we achieve this?

+3


source to share


2 answers


I also faced the same problem. While connecting with sequlize and Mysql, you can go through timezone

. Therefore, to save data, it will always use this one timezone

. Here's what I found:

var sequelize = new Sequelize('database', 'username', 'password', { 
    timezone: '+05:30', //here you can pass timezone
    host: 'host',
    logging: true,
});

      

But this is not a good solution, because if you are using your application from 2 different time zones, then it will use the same time zone for both, so the data will be wrong.




A good solution always uses UTC timezone:'00:00'

to save data and when retrieving results in the appropriate time zone format. This means that the user accessing the website from US

, manipulates the DateTime format US's

and shows, and if your access data is from India

then processes the DateTime format India's

and shows. To do this, you need to do some manipulation with DateTime. For this manipulation, you can use Moment.js .

+4


source


I have the same problem, I am storing data with a timezone of +00: 00. But I want to set the timezone to -03: 00 before doing the query. I've tried with PgAdmin and it works, but I can't seem to do it with sequelize. I want to count sales for a specific day, but first I have to change the time zone.



0


source







All Articles