![](/img/trans.png)
[英]Are there differences between [...array].map(...) and [...array.map(...)] in JavaScript?
[英]array.map Differences between Client and Node Server
我正在使用 moment.js 來轉換日期的格式。 執行以下代碼時,如果從客戶端執行,結果是正確的。 但是,如果我從 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"}]
Array.map 的行為在服務器和客戶端上沒有區別。
真正的問題是服務器和客戶端處理時間的方式不同。
通常,服務器時間設置基於UTC,客戶端遵循操作系統的系統時間。
按 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.