简体   繁体   中英

Sequelize convert date from UTC to local

I have a problem with queries in sequelize.js.

In database I store model with myDate field which is stored in UTC. I also have a query where time period is specified:

AND p.myDate BETWEEN :dateStart AND :dateEnd

Using sequelize.query , I replace parameters with:

  • dateStart: 2018-09-11T22:00:00.000Z
  • dateEnd: 2018-09-14T14:15:40.609Z

and now the problem is that I see query is executing however with localTime values:

AND p.myDate BETWEEN '2018-09-12 00:00:00' AND '2018-09-14 16:15:40'

Why did it convert it to the local time? I was not able to find the right answer.

You can specify the time zone in which you want to read/write in the config of Sequelize like this.

development: {
    username: 'postgres',
    password: 'postgres',
    database: 'your_database_name',
    host: '127.0.0.1',
    port: 5432,
    dialect: 'postgres',
    dialectOptions: {
      useUTC: false, // for reading from database
    },
    timezone: '+05:30', // for writing to database
  },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM