简体   繁体   English

array.map 客户端和节点服务器的区别

[英]array.map Differences between Client and Node Server

I am using moment.js to transform the format of a date.我正在使用 moment.js 来转换日期的格式。 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?但是,如果我从 Node 服务器运行完全相同的代码,它就不起作用了……会发生什么?

     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. Array.map 的行为在服务器和客户端上没有区别。

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.通常,服务器时间设置基于UTC,客户端遵循操作系统的系统时间。

Search by UTC, timezone, offset, etc.按 UTC、时区、偏移量等搜索。

"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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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