Sequelize.js 中的 UTC 日期

编程入门 行业动态 更新时间:2024-10-28 10:33:06
本文介绍了Sequelize.js 中的 UTC 日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 node.js 中使用 Sequelize 来保存打开和关闭时间.我也在使用 moment.js 来格式化.更改 FindOrCreate 我正在这样做:

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() ...

这工作正常,时间被格式化为 MySQL 的日期时间格式.问题是当我检索 Seqquelize 认为它是 UTC 时间并将其转换为 EST(我的服务器时区)的时间时.

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).

我希望它以 UTC 的形式进入数据库并以相同的方式出现.有什么我做错了吗?为什么 Sequelize 不会在插入时将其转换为 UTC,而是假定它是 UTC 出来的?另外,有没有办法让它不尝试转换为我的服务器时区?

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?

推荐答案

我也遇到了同样的问题.我已经这样解决了:

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):

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.... :)

更多推荐

Sequelize.js 中的 UTC 日期

本文发布于:2023-10-23 08:12:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1520283.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   Sequelize   js   UTC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!