lhpkhanh lhpkhanh - 3 months ago 59
Node.js Question

Sequelize went wrong when saved date in nodejs

I tried to store an object to Mysql with Sequelize, Nodejs. Every things did ok, excepted the timestamp went wrong. There are my lines:

var log = {
id: id,
error_code: error_code,
created_at: moment().format('YYYY-MM-DD HH:mm:ss'),
updated_at: moment().format('YYYY-MM-DD HH:mm:ss')
console.log('Create Log');

Focus on these two timestamp fields, I have logged and saw it was right when I created it.

var log = {
created_at: '2016-11-16 22:51:24',
updated_at: '2016-11-16 22:51:24'

But, It is wrong in the mysql record:

created_at: '2016-11-17 05:45:34',
updated_at: '2016-11-17 05:45:34'

In MySql, these fields are defined as:

- created_at: timestamp, nullable
- updated_at: timestamp, nullable

It is very basic, so I don't know what happened. It's so strange!
Where is my mistake?


Use the Sequelize options.timezone property when creating your database connection to make sure that the application and MySQL server are both set to the same timezone offset.



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.