Momentjs更改时区而不会影响时间

发布于 2025-02-09 08:18:13 字数 374 浏览 2 评论 0原文

我正在尝试将时区设置为“瞬间”日期。

我在UTC中得到一个约会:

const date = "2022-01-18T00:00:00.000Z";

我要做下一个时刻区,

moment(date).tz("America/Bogota", true).format();

它应该改变时区而不会影响时间,但我得到了:

2022-01-17T19:00:00-05:00

,我期望的是:

2022-01-18T00:00:00-05:00

I'm trying to set the timezone for a date in moment.js without changing the time value.

I get a date in utc:

const date = "2022-01-18T00:00:00.000Z";

And Im doing the next with moment-timezone

moment(date).tz("America/Bogota", true).format();

It is supposed to change the timezone without affecting the time but im getting:

2022-01-17T19:00:00-05:00

And what I expect:

2022-01-18T00:00:00-05:00

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

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

发布评论

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

评论(3

梦途 2025-02-16 08:18:13

我建议也许使用armoent.tz构造函数带有格式字符串。这样,我们将分析输入日期作为当地时间。

const date = "2022-01-18T00:00:00.000Z";

console.log(moment.tz(date, "YYYY-MM-DD[T]HH:mm:sss.SSS", "America/Bogota").format());
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>

I'd suggest perhaps using the moment.tz constructor with a format string. This way, we'll parse the input date as a local time.

const date = "2022-01-18T00:00:00.000Z";

console.log(moment.tz(date, "YYYY-MM-DD[T]HH:mm:sss.SSS", "America/Bogota").format());
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data-1970-2030.min.js"></script>

筱果果 2025-02-16 08:18:13

我以这种方式修复了:

moment.utc(date).tz("America/Bogota", true).format();

I fixed this way:

moment.utc(date).tz("America/Bogota", true).format();
帅的被狗咬 2025-02-16 08:18:13

似乎.format可能正在将日期转换为本地时间。
中获得的各个时区的结果

我在矩

您提供的从UTC转换为当地时间的日期

使用我的本地moment()

It seems as though .format may be converting the date into local time.
The results I get with various time zones in moment

results

The date you provided converted from UTC to my local time
utc converted

With my local moment()
local time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文