简体   繁体   中英

array.map Differences between Client and Node Server

I am using moment.js to transform the format of a date. When executing the following code, if it is executed from the client, the result is correct. But if I run the exact same code from the Node server, it doesn't work... What could be happening?

     const data = [{id:1,date: "2021-06-21T00:00:00.000Z"},{id:2,date: "2021-06-22T00:00:00.000Z"},{id:3,date: "2021-06-23T00:00:00.000Z"}]
     const result = data.map( x => {
              x.date= moment(x.date).format('DD/MM/YYYY');
              return x
              });
    
      console.log(result)

    //Return from client (Chrome, Firefox):
    [{id:1,date: "21/06/2021"},{id:2,date: "22/06/2021"},{id:3,date: "23/06/2021"}]

    //Return from Node.js:
    [{id:1,date: "2021-06-21T00:00:00.000Z"},{id:2,date: "2021-06-22T00:00:00.000Z"},{id:3,date: "2021-06-23T00:00:00.000Z"}]

The behavior of Array.map is not different on server and client.

The real problem is that the server and the client handle time differently.

Usually, the server time is set based on UTC and the client follows the system time of the OS.

Search by UTC, timezone, offset, etc.

"new Date()" on the browser and on the server will give different results.

[browser]
result > Fri Jun 04 2021 09:37:57 GMT+0900

[server]
result > 2021-06-04T00:38:03.316Z

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM