在python中从日期时间减去秒 [英] Subtract seconds from datetime in python
本文介绍了在python中从日期时间减去秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个实际上是秒的int变量(让我们称秒数 X
)。结果需要得到当前日期和时间(以日期时间格式)减去 X
秒。
I have an int variable that are actually seconds (lets call that amount of seconds X
). I need to get as result current date and time (in datetime format) minus X
seconds.
如果 X
为65,当前日期为 2014-06-03 15:45:00
,那么我需要得到结果 2014-06-03 15:43:45
。
If X
is 65 and current date is 2014-06-03 15:45:00
, then I need to get the result 2014-06-03 15:43:45
.
我正在Python 3.3.3上执行此操作,我知道我可能会使用 datetime
模块,但到目前为止还没有成功。
I'm doing this on Python 3.3.3 and I know I could probably use the datetime
module but I haven't had any success so far.
推荐答案
使用 datetime
模块确实是:
import datetime
X = 65
result = datetime.datetime.now() - datetime.timedelta(seconds=X)
您应该阅读文档,以了解如何使用它!
You should read the documentation of this package to learn how to use it!
这篇关于在python中从日期时间减去秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文