使用时区转换日期为UTC?

发布于 2025-02-13 20:13:48 字数 265 浏览 2 评论 0原文

一个快速的问题。我有一个ISO字符串日期:

2022-07-03T10:51:09+02:00

您可以看到的这个日期包括时区(+02:00)。

问题:如何将其转换为utc日期?使用Eg date-fnsmoment

编辑:我应该只是将“ 02:00”小时添加到当前日期?因此,它将是12:51:09

A quick question. I have a ISO string date:

2022-07-03T10:51:09+02:00

this date as you can see has timezone included (+02:00).

Question: How to convert it into UTC date? Using e.g. date-fns or moment?

Edit: Should I just simply add "02:00" hours to current date? So it would be 12:51:09?

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

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

发布评论

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

评论(2

水晶透心 2025-02-20 20:13:49

trivlively noreferrer“> new Date(IsoString)。 toisostring() ,无需库。

const input = "2022-07-03T10:51:09+02:00";
console.log(`${input} in UTC:\n${new Date(input).toISOString()}`);

Trivially new Date(isoString).toISOString(), no libraries required.

const input = "2022-07-03T10:51:09+02:00";
console.log(`${input} in UTC:\n${new Date(input).toISOString()}`);

何以畏孤独 2025-02-20 20:13:49

在我看来,时区只是在不同地理位置上的同一时间戳的表示(由于Unix时间0到处都是相同的秒数,因此经过的秒数不超过)。因此,请小心从现有时间戳手动添加/删除时间。

您可以使用moment.js这样做:

var someday = moment('2022-07-03T10:51:09+02:00');
console.log(someday.utc().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js"></script>

In my view, timezone is simply the representation of the same timestamp across different geographies (no. of seconds elapsed since unix time 0 is the same everywhere). So be careful while adding/removing time manually from the existing timestamp.

You can do that using moment.js like this:

var someday = moment('2022-07-03T10:51:09+02:00');
console.log(someday.utc().format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment-with-locales.min.js"></script>

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