Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

node.js - UTC DATE in Sequelize.js

I'm using Sequelize in node.js to save open and close times. I'm also using moment.js to format. Alter a FindOrCreate I'm doing this:

result.open = moment(hours.open, "hh:mma").format("YYYY-MM-DD HH:mm:ss");
result.save()
...

This works fine and the time gets formatted to MySQL's datetime format. The Problem is when I retrieve the time Seqquelize thinks it's a UTC time and converts it to EST (my server timezone).

I would prefer it to go into the database as UTC and come out the same way. Is there something I'm doing wrong? Why does Sequelize not convert it to UTC on insert but assumes it's UTC coming out? ALso, is there a way to not have it try to convert to my server timezone?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I had the same problem. i've fixed it like this:

config/sequelize.js:

const sequelize = new Sequelize(database, user, password, {
host,
dialect: 'mysql',
port,
operatorsAliases,
dialectOptions: {
    useUTC: true, // -->Add this line. for reading from database
},
timezone: '+02:00', // -->Add this line. for writing to database
pool: {
    max: 10,
    min: 0,
    idle: 10000,
},
logging: console.log,
// define: {},
})

momentJS (UTC):

const ACCEPT_FORMAT = 'YYYY-MM-DD hh:mm:ss'
const { start_date, end_date } = req.params
const start = moment.utc(start_date, ACCEPT_FORMAT)
const end = moment.utc(end_date, ACCEPT_FORMAT)
console.log(start)
console.log(end)

Hope it will help someone.... :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...