NodeJS MYSQL SELECT日期时间返回错误的日期

h4cxqtbf  于 2023-01-04  发布在  Node.js
关注(0)|答案(2)|浏览(145)

我正在做一个关于假期的项目,我遇到了一个奇怪的问题,我在服务器端使用NodeJS,在客户端使用React,在我将新的假期添加到数据库后,它将它完美地存储在MYSQL中(我目前正在使用PhpMyAdmin),我在PhpMyAdmin中检查了时区,它是+02:00,这是正确的。
以下是MYSQL数据库中的值:

vacationId: 12
description: D2
destination: D
flightDate: 2020-07-20 00:00:00
returnDate: 2020-07-21 00:00:00
price: 2

而我在客户端得到的日期是:

Flight date: 2020-07-19T21:00:00.000Z
Return date: 2020-07-20T21:00:00.000Z

下面是我如何添加假期:

let [flightDate, setFlightDateState] = React.useState(new Date());
let [returnDate, setReturnDateState] = React.useState(new Date());

const setFlightDate = (args: ChangeEvent<HTMLInputElement>) => {
    flightDate = new Date(args.target.value);
    setFlightDateState(flightDate)
}
const setReturnDate = (args: ChangeEvent<HTMLInputElement>) => {
    returnDate = new Date(args.target.value);
    console.log(returnDate);
    setReturnDateState(returnDate)
}

let vacationToAdd = new VacationsModel(null, description, destination, flightDate, returnDate, price);
axios.post<VacationsModel>("http://localhost:3000/api/vacations/addVacation", vacationToAdd);

下面是在服务器端获取假期的逻辑:

async function getAllVacationsAsync() {
    const sql = "SELECT * FROM vacationslist";
    const vacations = await dal.executeAsync(sql);
    return vacations ;
}

下面是在服务器端获取假期的控制器:

router.get("/", async (request, response) => { 
try {
    const vacations = await vacationsLogic.getAllVacationsAsync();
    response.json(vacations);
}
catch (err) {
    response.status(500).send(err.message);
}
});
ghhaqwfi

ghhaqwfi2#

您可以在createConnection函数中指定时区“+00:00

mysql.createConnection({
        host: 'your host',
        database: 'your database',
        user: 'your user',
        password: 'your password',
        timezone : "+00:00"
    });

相关问题