关于node.js:使用NodeJS插入MySQL日期

 2021-04-09 

Insert MySQL date using NodeJS

我想使用NodeJS插入一个MySQL日期,但是它不起作用。

我的新date:1989-12-31 00:00:00

在NodeJS插入之后:1989-12-30 23:00:00

NodeJS插入:

1
2
3
var user = request.body.user;
user.birth = user.birth.year + '-' + user.birth.month + '-' + user.birth.day + ' 00:00:00';
this.userDao.save(user);

使用Javascript的new Date()的结果:

1
Thu Dec 08 2016 11:00:15 GMT+0100 (Paris, Madrid)

如果我添加插入日期的一小时,则可以使用。


您需要在Sequelize中设置options.timezone,否则它将使用数据库服务器的时区偏移量(在您的情况下,这似乎是格林尼治标准时间),或者是距巴黎一小时的偏移量。

[options.timezone='+00:00']

The timezone used when converting a date from the database into a JavaScript date. The timezone is also used to SET TIMEZONE when connecting to the server, to ensure that the result of NOW, CURRENT_TIMESTAMP and other time related functions have in the right timezone. For best cross platform performance use the format +/-HH:MM. Will also accept string versions of timezones used by moment.js (e.g. 'America/Los_Angeles'); this is useful to capture daylight savings time changes.

同步连接

1
const sequelize = new Sequelize(db, user, pass, { timezone: 'Europe/Paris' });