How to pass a DateTime from NodeJS Sequelize to MSSQL

后端 未结 3 1251
走了就别回头了
走了就别回头了 2021-02-07 09:32

I have a NodeJS project, and I am trying to pass an \'UpdateDate\' field using Sequelize. I am receiving the error \'Conversion failed when converting date and/or time from char

3条回答
  •  渐次进展
    2021-02-07 09:48

    This is caused by a known issue in Sequelize. The solution is to patch Sequelize's date to string format implementation, like explained here, so that all dates are handled properly. Below is the code that fixes the error.

    const Sequelize = require('sequelize');
    
    // Override timezone formatting for MSSQL
    Sequelize.DATE.prototype._stringify = function _stringify(date, options) {
      return this._applyTimezone(date, options).format('YYYY-MM-DD HH:mm:ss.SSS');
    };
    

提交回复
热议问题