JS-将“欧洲/柏林”转换为UTC-Timestamp
在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从根本上讲,只有日期值不能转换为时间戳,除非您任意将A time 与该日期关联。听起来您打算使用午夜的当地时间(00:00+02:00),但您正在看到Midnight UTC(00:00z)。
这是由于您如何构造
date
对象。您指定new Date(“ 2022-04-20”)
,根据 Ecmascript Spec 将被视为午夜UTC。规格说:是的,这与ISO 8601不一致,并且已经讨论了Ad Nauseum。
要解决此问题,请将
t00:00
附加到输入字符串,以便您特别要求当地时间。换句话说,新日期(“ 2022-04-20T00:00”)
。就是说,如果您需要它不是“本地时间”,而是
欧洲/柏林
,那么 - 您需要使用库。在卢克森,就是这样: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 specifynew Date("2022-04-20")
, which according to the ECMASCript spec will be treated as midnight UTC. The spec says: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: