javascript - new Date() 返回错误的日期
问题描述
我知道这是一个非常基本的问题,但我坚持了几个小时。当我将 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 的情况下没有变化。
希望上面的例子能澄清它。
推荐阅读
- flutter - 错误“QueryDocumentSnapshot”不是“index”的“int”类型的子类型
- google-chrome - Why does Chrome webrtc not support iLBC audio codec now
- spring - 用于 Spring Boot 休息控制器的 Junit
- excel - 相对地址与绝对地址
- javascript - 有没有办法在过滤时获取过滤后数组的索引
- google-apps-script - 删除A列中的单元格时如何删除单个行中的多个单元格
- django-urls - 如何在视图中反转 Django 管理模板的 URL?
- python - 我将如何规范嵌套的 json
- angular - 我无法在 Agular 10 中更新服务中的值
- ios - 使用 Swift、Metal 和片段着色器在 iOS 上绘制椭圆