How to pass a DateTime from NodeJS Sequelize to MSSQL(如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL)
问题描述
我有一个 NodeJS 项目,我正在尝试使用 Sequelize 传递一个UpdateDate"字段.我收到错误从字符串转换日期和/或时间时转换失败".我尝试过传递一些不同的东西:
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 character string'. I have tried passing a few different things:
Date.now()
new Date().toISOString()
都不行.我错过了一些简单的东西吗?我无法更改表上的列定义.据我所知,将诸如 '2016-05-23 10:39:21.000' 之类的字符串传递给 SQL DateTime 字段在 SSMS 中有效,但在使用 Sequelize 和 Node.js 时似乎是一个问题.
Neither work. Am I missing something simple? I cannot change the column definition on the table. As far as I know, passing a string such as '2016-05-23 10:39:21.000' to a SQL DateTime field works in SSMS, but it seems to be an issue when using Sequelize and Node.
谢谢
扎克
推荐答案
这是由 已知的Sequelize 中的问题.解决方案是将 Sequelize 的日期修补为字符串格式实现,如 在此处解释,以便正确处理所有日期.下面是修复错误的代码.
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');
};
这篇关于如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 NodeJS Sequelize 中的 DateTime 传递给 MSSQL
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 是否可以执行按位分组功能? 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
