【问题标题】:Why is the date in the request body different from the date in the response body in postman为什么请求正文中的日期与邮递员响应正文中的日期不同
【发布时间】:2021-09-14 20:43:03
【问题描述】:

我正在尝试在邮递员的请求正文中发布日期

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

但身体响应会产生输出

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

}

我使用 node.js express 作为 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();
  })

},

我希望日期格式与请求正文相同,例如 date_borrow: 2021-01-01 date_return: 2021-02-01

【问题讨论】:

  • 您将日期发回哪里?我只能看到 res.send 中的成功部分
  • 我将日期发送到mysql数据库类型日期,但是当在邮递员中获取数据时,日期结果与数据库不匹配

标签: mysql node.js express date postman


【解决方案1】:

您似乎使用 postgresql 作为数据库,而 postgresql 将日期存储在 UTC +00 中。

选择时区:

SELECT * FROM pg_timezone_names;

并设置如下给出的示例:

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

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

【讨论】:

  • 我用mysql数据库
  • 我在mysql数据库上试了一下,结果报错
  • 抱歉我之前没用过mysql
【解决方案2】:

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

【讨论】:

  • 浏览器会自动处理。让它保持原样并在浏览器中测试它。
  • 在浏览器中仍然产生相同的响应正文日期
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-30
  • 1970-01-01
  • 2020-03-11
  • 2015-05-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多