从Python中的日期减去n天[复制]

时间:2022-08-25 19:00:15

This question already has an answer here:

这个问题在这里已有答案:

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 filedate works, so that date is read correctly from the files. It's the subtraction bit that doesn't seem to be working.

打印存档工作正常,以便从文件中正确读取日期。它是减法位似乎不起作用。

Desired output: If filedate is 06/11/2013, print end_date should yield 06/01/2013.

期望的输出:如果提交的是06/11/2013,则打印end_date应该会产生06/01/2013。

2 个解决方案

#1


7  

When you use time.strftime() you are actually converting a struct_time to a string.

使用time.strftime()时,实际上是将struct_time转换为字符串。

so filedate is actually a string. When you try to + or - a datetime.timedelta from it, you would get an error. Example -

所以申请实际上是一个字符串。当你尝试+或 - 来自它的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.

要获得类似于time.gmtime()的行为,可以改为使用datetime.datetime.utcfromtimestamp(),这将提供一个datetime对象,您可以从中减去timedelta。

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

#2


10  

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 

#1


7  

When you use time.strftime() you are actually converting a struct_time to a string.

使用time.strftime()时,实际上是将struct_time转换为字符串。

so filedate is actually a string. When you try to + or - a datetime.timedelta from it, you would get an error. Example -

所以申请实际上是一个字符串。当你尝试+或 - 来自它的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.

要获得类似于time.gmtime()的行为,可以改为使用datetime.datetime.utcfromtimestamp(),这将提供一个datetime对象,您可以从中减去timedelta。

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

#2


10  

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