我如何找到两个时区之间小时的差异?
我在这里有这个代码,目前可以在两个不同的时区中找到时间。我想计算两个时区之间的小时差。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
适用于pytz时区(例如“我们/东部”,“欧洲/莫斯科”)正确的工作。
Works correct for pytz timezones such as "US/Eastern", "Europe/Moscow".
我会做两个嵌套的循环,以将您的时间分为两个空间,然后“:”,以便您可以获取单个数字。然后,它只是减法,也许是字典,如果您想说的是“ 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"