new Date() vs Date() and why does it return a different time (-2 hours)?

前端 未结 2 1324
情话喂你
情话喂你 2021-01-14 00:19

I have these 2 console logs, but they return different times (-2 hours off).

console.log(new Date()) // Date 2015-04-20T15:37:23.000Z
console.log(Date()) //          


        
2条回答
  •  被撕碎了的回忆
    2021-01-14 01:19

    Those are the same time. The string generated by Date() just uses a different timezone, as can be seen from the GMT+0200 (CEST) suffix.

    So if you need your time string in your local timezone, just use .toString() instead of .toUTCString().

提交回复
热议问题