我知道这是一个非常基本的问题,但我坚持了几个小时。当我将 ISO 字符串传递给时,为什么会得到错误的日期和时间new Date()
new Date('2017-08-01T00:00:00');
=> 2017-07-31T22:00:00.000Z
我知道这是一个非常基本的问题,但我坚持了几个小时。当我将 ISO 字符串传递给时,为什么会得到错误的日期和时间new Date()
new Date('2017-08-01T00:00:00');
=> 2017-07-31T22:00:00.000Z
new Date('2017-08-01T00:00:00').toISOString() => 2017-07-31T18:30:00.000Z (my timezone is +530)
new Date('2017-08-01T00:00:00.000Z').toISOString() => 2017-08-01T00:00:00.000Z (input is in UTC)
new Date('2017-08-01T00:00:00.000+0530').toISOString() => 2017-07-31T18:30:00.000Z
new Date('2017-08-01T00:00:00.000+0200').toISOString() => 2017-07-31T22:00:00.000Z
在您的情况下,输入日期不是 UTC,您的系统时区是 +0200,因此您可以看到时差。第二个示例显示在 UTC 的情况下没有变化。
希望上面的例子能澄清它。
返回的日期是 ISO 8601 日期。这是个好日子:)
const test = new Date('2017-08-01T00:00:00');
const isoString = test.toISOString()
const dateString = test.toDateString()
console.log('iso', isoString)
console.log('date', dateString)