如何将DateJ从NodeJS Sequelize传递给MSSQL [英] How to pass a DateTime from NodeJS Sequelize to MSSQL

查看:170
本文介绍了如何将DateJ从NodeJS Sequelize传递给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时似乎是一个问题。

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.

谢谢

Zach

推荐答案

这是由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');
};

这篇关于如何将DateJ从NodeJS Sequelize传递给MSSQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆