python - 自一天 UTC 时区开始以来的秒数

标签 python utc timedelta seconds

如何在 Python 中找到“自 UTC 时区开始以来的秒数”?我查看了文档,但不明白如何使用 datetime.timedelta 获取它。

最佳答案

这是一种方法。

from datetime import datetime, time

utcnow = datetime.utcnow()
midnight_utc = datetime.combine(utcnow.date(), time(0))
delta = utcnow - midnight_utc
print delta.seconds # <-- careful

编辑 如建议的那样,如果您想要微秒精度,或者可能跨越 24 小时周期(即 delta.days > 0),请使用 total_seconds() 或@unutbu 给出的公式。

print delta.total_seconds()  # 2.7
print delta.days * 24 * 60 * 60 + delta.seconds + delta.microseconds / 1e6 # < 2.7

关于python - 自一天 UTC 时区开始以来的秒数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8072740/

相关文章:

python - Pandas:通过在列中查找子字符串改进算法

java - 在不使用弃用代码的情况下从 YMD HMS 值创建 Java 日期

sql - Oracle SQL : converting timestamp to UTC

c# - 在本地时区格式化 UTC 时间戳

python - numpy.longdouble dtype 的 timedelta 错误

python - 如何在 Python 中使用 os.umask()

python - Pandas 基于索引/列组合合并 DataFrame

python - Django 表单 EmailField 不接受 css 属性

python-3.x - Python/Pandas 中是否有函数可以获取两个日期时间之间的业务时间增量?

Python 格式 timedelta 大于 24 小时以显示仅包含小时数?