It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center 。
10年前关闭。
我有两个这样的日期时间字符串 '2010-08-31 04:35:50.176725' 和 '2010-09-05 04:35:50.176725' 。现在我的问题是如何计算两个日期之间的秒数。我使用时间增量,但它以小时、分钟格式返回。我想在几秒钟内完成。
纪元定义为 1970-01-01 00:00:00 GMT。
您可以通过以下方式找到
或者,如果您想查找两个 dt.datetime 对象之间的秒数:
10年前关闭。
我有两个这样的日期时间字符串 '2010-08-31 04:35:50.176725' 和 '2010-09-05 04:35:50.176725' 。现在我的问题是如何计算两个日期之间的秒数。我使用时间增量,但它以小时、分钟格式返回。我想在几秒钟内完成。
最佳答案
import datetime as dt
import time
now=dt.datetime.now()
纪元定义为 1970-01-01 00:00:00 GMT。
您可以通过以下方式找到
now
和 Epoch 之间的秒数:print(time.mktime(now.timetuple()))
# 1289565310.0
或者,如果您想查找两个 dt.datetime 对象之间的秒数:
now2=dt.datetime(2010,11,12,12,0,0)
def timestamp(date):
return time.mktime(date.timetuple())
print(timestamp(now2)-timestamp(now))
# 15890.0
关于python - 日期时间到秒的转换,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4164518/