python - 如何在python中将本地时间转换为UTC时间
问题描述
我正在尝试将本地时间转换为 UTC 时间。但得到以下错误。
Error: an integer is required (got type str)
from datetime import datetime
starts_date = '2021-07-30 09:30:00'(timestamp without time zone)
ts = starts_date
x = datetime.utcfromtimestamp(ts)
x_ts = x.timestamp()
解决方案
这是一个将(现在)的时间戳转换为 UTC 时间戳的工作示例。
from datetime import datetime as dt
# Create a timestamp object for now.
ts = dt.timestamp(dt.now())
# Convert now to a UTC timestamp.
dt.utcfromtimestamp(ts).timestamp()
>>> 1627637013.657752
推荐阅读
- sqlite - 如何在 xamarin 中执行 Database.QueryAysnc
- java - 从嵌套的哈希图中获取、放置键和值
- javascript - 如何从数组创建键值对?
- excel - 使用 Power Shell 创建电子表格并重命名选项卡
- python - 如何在 Pandas DataFrame 中使用列值作为标题
- javascript - 数组中所有先前存储的对象都将替换为数组末尾新插入的对象。为什么?
- git - 处理来自 tfs 服务器的 Git 存储库更新
- amazon-web-services - 在 S3 上解压缩大量文件
- python - 搜索字符串并删除匹配行
- python - 如何使用 Pygame 快速渲染 Julia Set?