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


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- SQL 临时表问题 2022-01-01