JS-将“欧洲/柏林”转换为UTC-Timestamp

发布于 2025-01-30 07:59:57 字数 728 浏览 2 评论 0原文

在我的Docker-container内部,具有TimeZone etc/utc,我需要转换一个代表欧洲/柏林 -TimeZone中日期的日期弦乐, 。

因此,可以说欧洲/柏林 -date是2022-04-20T00:00:00:00

现在,UTC-Timestamp应等于2022-04-19T22:00:00

但是,当我这样做时,

console.log(new Date("2022-04-20").getTime())

我得到1650412800000等同于2022-04-20T02:00:00:00:00 in 欧洲/berlin -TimeZone。

我该怎么做?

编辑:

我尝试了各种Libs,但仍然无法

const { DateTime } = require("luxon")


var f = DateTime.fromISO("2022-04-20").setZone('Europe/Berlin').toUTC()
console.log(f)

f IS 2022-04-20T02:00:00:00:/

Inside my Docker-Container, which has the timezone Etc/UTC, I need to convert a Date-String which represents a Date in Europe/Berlin-timezone into a UTC timestamp.

So lets say the Europe/Berlin-Date is 2022-04-20T00:00:00.

Now the UTC-Timestamp should be equivalent to 2022-04-19T22:00:00.

But when I do

console.log(new Date("2022-04-20").getTime())

I get 1650412800000 which is equivalent to 2022-04-20T02:00:00 in Europe/Berlin-timezone.

How would I do this?

Edit:

I tried various libs, but still cant get that managed

const { DateTime } = require("luxon")


var f = DateTime.fromISO("2022-04-20").setZone('Europe/Berlin').toUTC()
console.log(f)

also the equivalent stamp in f is 2022-04-20T02:00:00 :/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自我难过 2025-02-06 07:59:57

我需要将代表欧洲/柏林时区日期的日期弦转换为UTC时间戳。

从根本上讲,只有日期值不能转换为时间戳,除非您任意将A time 与该日期关联。听起来您打算使用午夜的当地时间(00:00+02:00),但您正在看到Midnight UTC(00:00z)。

这是由于您如何构造date对象。您指定new Date(“ 2022-04-20”),根据 Ecmascript Spec 将被视为午夜UTC。规格说:

...当不存在UTC偏移表示形式时,仅将日期表格解释为UTC时间和日期时间表格被解释为本地时间。

是的,这与ISO 8601不一致,并且已经讨论了Ad Nauseum。

要解决此问题,请将t00:00附加到输入字符串,以便您特别要求当地时间。换句话说,新日期(“ 2022-04-20T00:00”)

就是说,如果您需要它不是“本地时间”,而是 欧洲/柏林,那么 - 您需要使用库。在卢克森,就是这样:

DateTime.fromISO('2022-04-20T00:00', {zone: 'Europe/Berlin'}).toUTC()

I need to convert a Date-String which represents a Date in Europe/Berlin-timezone into a UTC timestamp.

Fundamentally, a date-only value cannot be converted into a timestamp, unless you arbitrarily associate a time with that date. It sounds like you meant to use midnight local time (00:00+02:00) but instead you are seeing midnight UTC (00:00Z).

This is due to how you are constructing the Date object. You specify new Date("2022-04-20"), which according to the ECMASCript spec will be treated as midnight UTC. The spec says:

... When the UTC offset representation is absent, date-only forms are interpreted as a UTC time and date-time forms are interpreted as a local time.

Yes, this is inconsistent with ISO 8601, and that has been discussed ad nauseum.

To solve this problem, append T00:00 to your input string, so that you are specifically asking for local time. In other words, new Date("2022-04-20T00:00").

That said, if you need it to not be "local time" but exactly Europe/Berlin, then yes - you'll need to use a library. In luxon, it's like this:

DateTime.fromISO('2022-04-20T00:00', {zone: 'Europe/Berlin'}).toUTC()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文