Python:将本地时间转换为另一个时区

标签 python time

我想在 Python 中将当前时间转换为 +0900。

执行此操作的合适方法是什么(假设在时间模块中)?

我读到它不包含在 Python 中,您必须使用 pytz 之类的东西。

我不想在服务器基础上或全局范围内更改它,只是在这一个实例中。

最佳答案

将数据从 UTC 转换为 IST 的示例

from datetime import datetime
from pytz import timezone

format = "%Y-%m-%d %H:%M:%S %Z%z"

# Current time in UTC
now_utc = datetime.now(timezone('UTC'))
print now_utc.strftime(format)
Output: 2015-05-18 10:02:47 UTC+0000

# Convert to Asia/Kolkata time zone
now_asia = now_utc.astimezone(timezone('Asia/Kolkata'))
print now_asia.strftime(format)
Output: 2015-05-18 15:32:47 IST+0530

关于Python:将本地时间转换为另一个时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5280692/

相关文章:

jquery - Django 。 ajax调用后数量不会增加

javascript - 对Python和Javascript的理解只是很基础?

mysql - 当我们有 n 次在 00 :00:00 format 中时,如何在 mysql 中添加时间列表

javascript - 时间到午夜 CST?

java - 比较 MySQL 和 Java 时间

python - ValueError : Error when checking target: expected activation_7 to have shape (154, 154, 1) 但得到形状为 (200, 200, 3) 的数组

python - 在 VS code python notebook 中将 autopep8 和 linting 添加到 jupyter

python - 在 contains 方法中使用精确匹配正则表达式

opengl-es - OpenGL ES 2.0着色器,时间变量是如何调用的?

php - 如何更改时间配置?