为什么邮递员中请求 body 中的日期与响应 body 中的日期不同

Why is the date in the request body different from the date in the response body in postman

我正在尝试 post body 请求中的日期 postman

date_borrow: 2021-01-01 date_return: 2021-02-01

但 body 响应产生输出

{
"success": true,
"message": "success",
"data": [
    {
        "date_borrow": "2020-12-31T16:00:00.000Z",
        "date_return": "2021-01-31T16:00:00.000Z"
    }
]

}

我使用 node.js 表示 rest-api

addBorrowingMember(req,res){
  let dataBorrowingMember = {
      date_borrow : req.body.date_borrow,
      date_return : req.body.date_return
  }

  pool.getConnection(function(err, connection) {
      if (err) throw err;
      connection.query(
          `
          INSERT INTO borrowing SET ?;
          `
          , [dataBorrowingMember],
      function (error, results) {
          if(error) throw error;
          res.send({
              success: true,
              message: 'success',
          });
      });
      connection.release();
  })

},

我希望日期格式与请求相同 body 像这样 date_borrow: 2021-01-01 date_return: 2021-02-01

因为你没有为 express 服务器指定时区,所以系统默认日期时间为 UTC ......所以你添加的时间(即使是从浏览器)与服务器保存的日期之间存在差异,它取决于您的时区和服务器 UTC 时区之间的差异。

您似乎使用 postgresql 作为数据库并且 postgresql 以 UTC +00 存储日期。

选择时区:

SELECT * FROM pg_timezone_names;

并设置如下示例:

ALTER DATABASE postgres SET timezone TO 'Europe/Berlin';

在上面的语句中使用您的数据库名称代替 postgres。