我如何找到两个时区之间小时的差异?

发布于 2025-01-28 21:18:58 字数 449 浏览 2 评论 0原文

我在这里有这个代码,目前可以在两个不同的时区中找到时间。我想计算两个时区之间的小时差。

def time_caluclator(timezone1, timezone2):

    dt_utcnow = datetime.datetime.now(tz=pytz.UTC)

    dt_1 = dt_utcnow.astimezone(pytz.timezone(timezone1))
    dt_2 = dt_utcnow.astimezone(pytz.timezone(timezone2))

    print(dt_1, dt_2)

这是代码,它将打印出来:(

2022-05-15 00:44:22.031149+00:00 2022-05-15 01:44:22.031149+01:00

第一个时区是Zulu,另一个是湿的)。

I have this code here that can find the time in two different timezones at the moment. I want to calculate the difference in hours, between the two timezones.

def time_caluclator(timezone1, timezone2):

    dt_utcnow = datetime.datetime.now(tz=pytz.UTC)

    dt_1 = dt_utcnow.astimezone(pytz.timezone(timezone1))
    dt_2 = dt_utcnow.astimezone(pytz.timezone(timezone2))

    print(dt_1, dt_2)

This is the code, and it will print this:

2022-05-15 00:44:22.031149+00:00 2022-05-15 01:44:22.031149+01:00

(First timezone is Zulu, and the other is WET).

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

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

发布评论

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

评论(2

温柔女人霸气范 2025-02-04 21:18:58
def time_caluclator(timezone1, timezone2):
    off1 = pytz.timezone(timezone1).utcoffset(datetime.datetime.now())
    off2 = pytz.timezone(timezone2).utcoffset(datetime.datetime.now())
    return (off2 - off1).seconds // 3600

适用于pytz时区(例如“我们/东部”,“欧洲/莫斯科”)正确的工作。

def time_caluclator(timezone1, timezone2):
    off1 = pytz.timezone(timezone1).utcoffset(datetime.datetime.now())
    off2 = pytz.timezone(timezone2).utcoffset(datetime.datetime.now())
    return (off2 - off1).seconds // 3600

Works correct for pytz timezones such as "US/Eastern", "Europe/Moscow".

深爱不及久伴 2025-02-04 21:18:58

我会做两个嵌套的循环,以将您的时间分为两个空间,然后“:”,以便您可以获取单个数字。然后,它只是减法,也许是字典,如果您想说的是“ EST”的时区代码

I would do two nested for loops to separate your times into sections by both spaces and ":" so that you can get the individual numbers. Then its just subtraction and maybe a dictionary if you would like to say specifically timezone codes like "EST"

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