mysql datetime and nodejs

问题描述 投票:0回答:1

我在Mysql中有一个表,它具有createdAt。那是日期时间类型。我想在数据库中保存UTC时间,并在浏览器中显示其本地时间。当我从NojeJS提取数据时,似乎将数据视为本地时间,并再次自动更改为UTC。

例如,我在Mysql中创建了一个数据,时间为:2020-04-15 21:05:08,但是一旦我获取它,它就会显示2020-04-15T18:05:08.000Z,这不是我想要的。我想念什么?

这些是我的获取数据的代码。

Mysqlhelper

const mysql = require("mysql");

const mysqlConn = mysql.createConnection({
    protocol: "mysql",
    host: 'localhost',
    port: 3306,
    user: "dbuser",
    database: 'crm'
});

module.exports = mysqlConn;

MessageDao

const mysqlConn = require("./mysqlhelper");
 const messagesDao = {
    getRelatedMessages(email,cb){
        mysqlConn.query("select * from messages where sender=? or receiver=? order by createdAt", [email,email], (error, data) => {
            console.log(data[0].createdAt);
            cb(error, data);
        });
    }
}

module.exports = messagesDao;
javascript mysql node.js utc
1个回答
0
投票

我认为服务器(nodeJS)将UTC格式返回到您的前端很好。前端/客户端应该是将其转换为本地时间的客户端。

© www.soinside.com 2019 - 2024. All rights reserved.