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

I’m trying to post the date in the request body in postman

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

but the body response produces output

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

}

i use node.js express for 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();
  })

},

I want the date format the same as the request body like this date_borrow: 2021-01-01 date_return: 2021-02-01

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

It seems that you use postgresql as database and postgresql stores date in UTC +00.

To choose a timezone:

SELECT * FROM pg_timezone_names;

And set as below given example:

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

Use your DB name in place of postgres in above statement.

Method 2

Because you did not specify a timezone for express server, so the system defaulted the datetime to UTC … so there is a difference between the time you add ( even from the browser ) to the date saved by the server , it depends on the difference between your timeZone and the server UTC timeZone .


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x