当前日期时间比Node.js慢6小时。

huangapple go评论63阅读模式
英文:

current datetime is 6 hours behind nodejs

问题

I need to compare the current datetime with datetime in the database column. In my computer new Date() returns datetime that is 2023-05-21T04:35:07.903Z, whereas my computer time is 10:35:07. How can I adjust this?

Note: I cannot convert them to string as I need to compare them.
My code:

console.log("current time", new Date());
console.log("db time", new Date(results[i].start));

if (new Date(results[i].start) >= new Date()){
    ..............
}
英文:

I need to compare the current datetime with datetime in database column. In my computer new Date() returns datetime that is 2023-05-21T04:35:07.903Z whereas My computer time is 10:35:07. How can I adjust this?

Note : I cannot convert them to string as I need to compare them.
My code :

        console.log("current time", new Date());
        console.log("db time", new Date(results[i].start));

        if ( new Date(results[i].start) >= new Date()){
            ..............
        }

答案1

得分: 1

以下是您要的翻译:

容易混淆的是 JavaScript Date

> JavaScript 日期基本上是以自1970年1月1日午夜(世界标准时间)起经过的毫秒数来定义的,这被定义为纪元时间,相当于UNIX纪元。此时间戳是与时区无关的,并唯一地定义了历史上的一个瞬间。

如果您的 JavaScript 引擎(浏览器或 Node.js)处于 +0600 时区(又称 UTC/GMT+6,例如在哈萨克斯坦),当您执行 new Date 时,您会得到一个与时区无关的瞬间,但在显示时必须根据时区显示,因此它可以以以下任意方式显示:

  • 上午 04:35 UTC
  • 上午 10:35 UTC+6

日期字符串末尾的 Z 后缀确实表示时间以世界标准时间表示。因此,“2023-05-21T04:35:07.903Z” 表示上午 4:35 在世界标准时间,例如在伦敦。以及上午 10:35 在哈萨克斯坦。

如果 results[i].start 的值是 "2023-05-21T04:35:07.903Z" 并且表示上午 04:35 在世界标准时间(例如在伦敦)/ 上午 10:35 在哈萨克斯坦,正如它应该的那样,那么您无需调整 new Date:当您在哈萨克斯坦的上午 10:00 执行 new Date 时,您会得到 04:00Z,因此在 results[i].start 之前。

new Date("2023-05-21T04:35:07.903Z") >= new Date() // 如果在 2023-05-21 上午 10 时在 UTC+6 执行,则为 true

相反,如果由于某种原因,您的数据库中的值 "2023-05-21T04:35:07.903Z" 应被解释为在哈萨克斯坦的上午 4:35,则确实您需要做以下之一:

  • 更改 new Date 的偏移量,以便您的本地时间也像在世界标准时间(而不是 UTC+6)一样被移动
  • 改变数据库值的偏移量,使其实际上现在表示 UTC+6 的时间

您可以通过省略 Z 后缀轻松实现后者,以便日期时间字符串不再被解释为以世界标准时间表示(因此它将被解释为以本地时间表示):

function dateConvertedToLocal(datetime) {
  const withoutZsuffix = datetime.replace(/Z$/i, "");
  return new Date(withoutZsuffix);
}

dateConvertedToLocal("2023-05-21T04:35:07.903Z") >= new Date // 如果在 2023-05-21 上午 10 时在 UTC+6 执行,则为 false
英文:

It is easy to be confused with JavaScript Date:

> A JavaScript date is fundamentally specified as the time in milliseconds that has elapsed since the epoch, which is defined as the midnight at the beginning of January 1, 1970, UTC (equivalent to the UNIX epoch). This timestamp is timezone-agnostic and uniquely defines an instant in history.

If your JavaScript engine (browser or Node.js) is in a +0600 timezone (a.k.a. UTC/GMT+6, e.g. in Kazakhstan), when you do new Date, you get an instant that is independant from timezone, but when expressed it must be displayed according to a timezone, so it can be displayed indifferently as:

  • 04:35 am UTC
  • 10:35 am UTC+6

The Z suffix at the end of the date string indeed indicates that the time is expressed in UTC timezone. Therefore "2023-05-21T04:35:07.903Z" means 4:35 am in UTC, e.g. in London. And 10:35 am in Kazakhstan.


If results[i].start value is "2023-05-21T04:35:07.903Z" and means 04:35 am in UTC (e.g. London) / 10:35 am in Kazakhstan, as it should, then you do not need to adjust new Date: when you do new Date at 10:00 am in Kazakhstan, you get 04:00Z, hence before results[i].start.

new Date("2023-05-21T04:35:07.903Z") >= new Date() // true if executed on 2023-05-21 at 10 am in UTC+6

If, on the contrary, and for whatever reason, the value "2023-05-21T04:35:07.903Z" in your database should be interpreted as 4:35 am in Kazakhstan, then indeed you need either to:

  • change the offset of new Date so that your local time is also shifted as if it were in UTC (instead of UTC+6)
  • shift the offset of the database value so that it now really means time in UTC+6

You could easily achieve the latter by omitting the Z suffix, so that the datetime string is no longer interpreted as expressed in UTC (hence it will be interpreted as expressed in local time instead):

function dateConvertedToLocal(datetime) {
  const withoutZsuffix = datetime.replace(/Z$/i, "");
  return new Date(withoutZsuffix);
}

dateConvertedToLocal("2023-05-21T04:35:07.903Z") >= new Date // false if executed on 2023-05-21 at 10 am in UTC+6

huangapple
  • 本文由 发表于 2023年5月21日 14:48:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76298637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定