从 Python 中的日期减去 n 天
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32334312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Subtracting n days from date in Python
提问by Isak
I want to subtract n days from a file's timestamp, but it doesn't seem to be working. I have read this post, and I think I'm close.
我想从文件的时间戳中减去 n 天,但它似乎不起作用。我已经阅读了这篇文章,我想我已经接近了。
This is an excerpt from my code:
这是我的代码的摘录:
import os, time
from datetime import datetime, timedelta
def processData1( pageFile ):
f = open(pageFile, "r")
page = f.read()
filedate = time.strftime('%m/%d/%Y', time.gmtime(os.path.getmtime(pageFile)))
print filedate
end_date = filedate - datetime.timedelta(days=10)
print end_date
Printing filedateworks, so that date is read correctly from the files. It's the subtraction bit that doesn't seem to be working.
打印filedate工作,以便从文件中正确读取日期。减法位似乎不起作用。
Desired output:
If filedateis 06/11/2013, print end_dateshould yield 06/01/2013.
期望输出:如果filedate是 06/11/2013,print end_date应该 yield 06/01/2013。
采纳答案by Anand S Kumar
When you use time.strftime()you are actually converting a struct_timeto a string.
当您使用时,time.strftime()您实际上是将 a 转换struct_time为字符串。
so filedateis actually a string. When you try to +or -a datetime.timedeltafrom it, you would get an error. Example -
所以filedate实际上是一个字符串。当您尝试使用+或-使用datetime.timedelta它时,您会收到错误消息。例子 -
In [5]: s = time.strftime('%m/%d/%Y', time.gmtime(time.time()))
In [6]: s
Out[6]: '09/01/2015'
In [8]: s - datetime.timedelta(days=10)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-8-fb1d19ed0b02> in <module>()
----> 1 s - datetime.timedelta(days=10)
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'
To get a similar behavior to time.gmtime()to can instead use datetime.datetime.utcfromtimestamp(), this would provide a datetime object, from which you can subtract the timedelta.
要获得与 to 类似的行为,time.gmtime()可以改为使用datetime.datetime.utcfromtimestamp(),这将提供一个日期时间对象,您可以从中减去时间增量。
And then if the end result you want is actually a string, you can use datetime.strftime()to convert it to string in required format. Example -
然后,如果您想要的最终结果实际上是一个字符串,则可以使用datetime.strftime()将其转换为所需格式的字符串。例子 -
import os
from datetime import datetime, timedelta
def processData1( pageFile ):
f = open(pageFile, "r")
page = f.read()
filedate = datetime.utcfromtimestamp(os.path.getmtime(pageFile)))
print filedate
end_date = filedate - timedelta(days=10)
print end_date #end_date would be a datetime object.
end_date_string = end_date.strftime('%m/%d/%Y')
print end_date_string
回答by taesu
cleaned up import
清理进口
from datetime import datetime, timedelta
start = '06/11/2013'
start = datetime.strptime(start, "%m/%d/%Y") #string to date
end = start - timedelta(days=10) # date - days
print start,end

