Node Sequelize - insert date “as is” without converting to UTC

僤鯓⒐⒋嵵緔 提交于 2019-12-25 00:33:15

问题


Im trying to run a simple query like

Insert into table (somedate) values ('2018-06-11 23:59:00')

but Sequelize executes it as

Insert into table (somedate) values ('2018-06-12 02:59:00')

Is there an option to set the date "as is" without converting or changing it in any way?


回答1:


You can actually tell sequelize not to convert it to UTC while reading and you can also provide a time zone for while writing

const sequelize = new Sequelize('db_name', 'postgres', 'postgres', {
  host: '127.0.0.1',
  dialectOptions: {
    useUTC: false //for reading from database
  },
  dialect: 'postgres',
  timezone: '+05:30', // for writng
}); 



回答2:


I've found a hacky solution for you. You have to edit a Sequelize file which is not always an option, but it may solve your problem.

Open sequelize/lib/data-types.js and you will see

DATE.prototype._stringify = function _stringify(date, options) {
  date = this._applyTimezone(date, options);

  return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
};

The Z in the date.format() function means "current time zone", try removing the Z :

return date.format('YYYY-MM-DD HH:mm:ss.SSS');


来源:https://stackoverflow.com/questions/50798083/node-sequelize-insert-date-as-is-without-converting-to-utc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!