'Conversion failed when converting date and/or time from character string mssql sequelize
I'm trying to use Sequelize and node js to connect to a MS Sql server database and i have the following error when i try to test the post route in Postman: "Conversion failed when converting date and/or time from character string." The field in the database has the datetime type and i don't know how to solve the conversion problem.
Here's the code where i'm trying to do the conversion:
async function createFeedback(UserId,feedback) {
    try{
        //eroare la data->trebuie rezolvata
        return await Feedback.create({
            Mesaj: feedback.Mesaj,
            Recenzie: feedback.Recenzie,
            DataFeedback:Date.parse(feedback.DataFeedback),
            UserId: UserId
  });
}
catch(e){
  return e.message;
}
}
Does anyone have any suggestion?
The Feedback entity is defined like this:
import db from '../dbConfig.js';
import Sequelize from 'sequelize';
const Feedback = db.define("Feedback", {
    FeedbackId: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false
    },
    Mesaj: {
        type: Sequelize.STRING,
        allowNull: false
    },
    Recenzie: {
        type: Sequelize.STRING,
        allowNull: false
    },
    DataFeedback: {
        type: Sequelize.DATE, 
        allowNull: false  
       
    },
    UserId: {
        type: Sequelize.STRING,
        allowNull: false
    }
})
export default Feedback;
Solution 1:[1]
Firstly, set your locale (according to your region):
const moment = require('moment')
require('moment/locale/fa')
moment.locale('fa');
Then in your createFeedback function, you'll have:
return await Feedback.create({
            Mesaj: feedback.Mesaj,
            Recenzie: feedback.Recenzie,
            DataFeedback: moment().format('YYYY-MM-DD HH:mm:ss.SSS'),
            UserId: UserId
  });
I tested the above code successfully. Hope this helped :)
Solution 2:[2]
This is formating issue of Sequelize for MSSQL database. Sequelize by putting the extra value '+00:00' that is not accepted by the MSSQL.
Need to override timezone formatting for MSSQL
Sequelize.DATE.prototype._stringify = function _stringify(date, options) {
  date = this._applyTimezone(date, options);
  // Z here means current timezone, _not_ UTC
  // return date.format('YYYY-MM-DD HH:mm:ss.SSS Z');
  return date.format('YYYY-MM-DD HH:mm:ss.SSS');
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | A. Nadjar | 
| Solution 2 | Mohammad Ali Rony | 
