I have a date "10/10/11(m-d-y)"
and I want to add 5 days to it using a Python script. Please consider a general solution that works on the month ends also.
我有一个日期“10/10/11(m-d-y)”,我想用Python脚本添加5天的时间。请考虑一个通用的解决方案,在月底也可以。
I am using following code:
我使用以下代码:
import re
from datetime import datetime
StartDate = "10/10/11"
Date = datetime.strptime(StartDate, "%m/%d/%y")
print Date
-> is printing '2011-10-10 00:00:00'
打印日期->正在打印“2011-10-10 00:00”
Now I want to add 5 days to this date. I used the following code:
现在我想在这个日期加5天。我使用了以下代码:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
返回这个错误:
name 'timedelta' is not defined
9 个解决方案
#1
355
The previous answers are correct but it's generally a better practice to do:
前面的答案是正确的,但通常是更好的做法:
import datetime
Then you'll have, using datetime.timedelta
:
然后使用datetime。timedelta:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
#2
86
Import timedelta
first.
进口timedelta第一。
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
today .today()将返回今天的datetime,您可能需要它
EndDate = Date + timedelta(days=10)
#3
12
I guess you are missing something like that:
我猜你漏掉了这样的东西:
from datetime import timedelta
#4
10
If you happen to already be using pandas, you can save a little space by not specifying the format:
如果您碰巧已经在使用熊猫,您可以通过不指定格式来节省一点空间:
import pandas as pd
startdate = "10/10/2011"
enddate = pd.to_datetime(startdate) + pd.DateOffset(days=5)
#5
8
Here is another method to add days on date using dateutil's relativedelta.
下面是另一种使用dateutil的相对论delta添加日期天数的方法。
from datetime import datetime
from dateutil.relativedelta import relativedelta
print 'Today: ',datetime.now().strftime('%d/%m/%Y %H:%M:%S')
date_after_month = datetime.now()+ relativedelta(days=5)
print 'After 5 Days:', date_after_month.strftime('%d/%m/%Y %H:%M:%S')
Output:
输出:
Today: 25/06/2015 15:56:09
今天:25/06/2015 15:56:09
After 5 Days: 30/06/2015 15:56:09
5天后:30/06/2015 15:56:09。
#6
7
Here is a function of getting from now + specified days
这是一个从现在开始+指定天数的函数
import datetime
def get_date(dateFormat="%d-%m-%Y", addDays=0):
timeNow = datetime.datetime.now()
if (addDays!=0):
anotherTime = timeNow + datetime.timedelta(days=addDays)
else:
anotherTime = timeNow
return anotherTime.strftime(dateFormat)
Usage:
用法:
addDays = 3 #days
output_format = '%d-%m-%Y'
output = get_date(output_format, addDays)
print output
#7
3
In order to have have a less verbose code, and avoid name conflicts between datetime and datetime.datetime, you should rename the classes with CamelCase names.
为了具有更少的详细代码,并避免datetime和datetime之间的名称冲突。datetime,您应该用CamelCase名称来重命名类。
from datetime import datetime as DateTime, timedelta as TimeDelta
So you can do the following, which I think it is clear.
你可以做下面的,我觉得很清楚。
date_1 = DateTime.today()
end_date = date_1 + TimeDelta(days=10)
Also, there would be no name conflict if you want to import datetime
later on.
此外,如果您希望稍后导入datetime,则不会出现名称冲突。
#8
2
If you want add days to date now, you can use this code
如果您想现在添加日期,您可以使用此代码
from datetime import datetime
from datetime import timedelta
date_now_more_5_days = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')
#9
0
This is my initial but more complete development for a generic dateadd function in python
这是我对python中的通用dateadd函数的初步但更完整的开发
import datetime
from dateutil.relativedelta import relativedelta
def dateadd(date, part ,value):
if part=='year':
result = date + (value * relativedelta(years = 1))
elif part == 'month':
result = date + (value * relativedelta(months = 1))
elif part == 'day':
result = date + (value * relativedelta(days = 1))
elif part == 'hour':
result = date + (value * relativedelta(hours = 1))
elif part == 'minute':
result = date + (value * relativedelta(minutes = 1))
elif part == 'second':
result = date + (value * relativedelta(seconds = 1))
return result
date = datetime.datetime.now()
print(date, "now")
print(dateadd(date,'year',2) ,"year',2", )
print(dateadd(date,'year',-2) ,"year',-2", )
print(dateadd(date,'month',2) ,"month',2", )
print(dateadd(date,'month',-2) ,"month',-2", )
print(dateadd(date,'day',2) ,"day',2", )
print(dateadd(date,'day',-2) ,"day',-2", )
print(dateadd(date,'hour',2) ,"hour',2", )
print(dateadd(date,'hour',-2) ,"hour',-2", )
print(dateadd(date,'minute',2) ,"minute',2", )
print(dateadd(date,'minute',-2),"minute',-2", )
print(dateadd(date,'second',2) ,"second',2", )
print(dateadd(date,'second',-2),"second',-2", )
Result:
2018-03-08 09:04:12.619699 now
2020-03-08 09:04:12.619699 year',2
2016-03-08 09:04:12.619699 year',-2
2018-05-08 09:04:12.619699 month',2
2018-01-08 09:04:12.619699 month',-2
2018-03-10 09:04:12.619699 day',2
2018-03-06 09:04:12.619699 day',-2
2018-03-08 11:04:12.619699 hour',2
2018-03-08 07:04:12.619699 hour',-2
2018-03-08 09:06:12.619699 minute',2
2018-03-08 09:02:12.619699 minute',-2
2018-03-08 09:04:14.619699 second',2
2018-03-08 09:04:10.619699 second',-2
[参考译文]:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,。
#1
355
The previous answers are correct but it's generally a better practice to do:
前面的答案是正确的,但通常是更好的做法:
import datetime
Then you'll have, using datetime.timedelta
:
然后使用datetime。timedelta:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
#2
86
Import timedelta
first.
进口timedelta第一。
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
today .today()将返回今天的datetime,您可能需要它
EndDate = Date + timedelta(days=10)
#3
12
I guess you are missing something like that:
我猜你漏掉了这样的东西:
from datetime import timedelta
#4
10
If you happen to already be using pandas, you can save a little space by not specifying the format:
如果您碰巧已经在使用熊猫,您可以通过不指定格式来节省一点空间:
import pandas as pd
startdate = "10/10/2011"
enddate = pd.to_datetime(startdate) + pd.DateOffset(days=5)
#5
8
Here is another method to add days on date using dateutil's relativedelta.
下面是另一种使用dateutil的相对论delta添加日期天数的方法。
from datetime import datetime
from dateutil.relativedelta import relativedelta
print 'Today: ',datetime.now().strftime('%d/%m/%Y %H:%M:%S')
date_after_month = datetime.now()+ relativedelta(days=5)
print 'After 5 Days:', date_after_month.strftime('%d/%m/%Y %H:%M:%S')
Output:
输出:
Today: 25/06/2015 15:56:09
今天:25/06/2015 15:56:09
After 5 Days: 30/06/2015 15:56:09
5天后:30/06/2015 15:56:09。
#6
7
Here is a function of getting from now + specified days
这是一个从现在开始+指定天数的函数
import datetime
def get_date(dateFormat="%d-%m-%Y", addDays=0):
timeNow = datetime.datetime.now()
if (addDays!=0):
anotherTime = timeNow + datetime.timedelta(days=addDays)
else:
anotherTime = timeNow
return anotherTime.strftime(dateFormat)
Usage:
用法:
addDays = 3 #days
output_format = '%d-%m-%Y'
output = get_date(output_format, addDays)
print output
#7
3
In order to have have a less verbose code, and avoid name conflicts between datetime and datetime.datetime, you should rename the classes with CamelCase names.
为了具有更少的详细代码,并避免datetime和datetime之间的名称冲突。datetime,您应该用CamelCase名称来重命名类。
from datetime import datetime as DateTime, timedelta as TimeDelta
So you can do the following, which I think it is clear.
你可以做下面的,我觉得很清楚。
date_1 = DateTime.today()
end_date = date_1 + TimeDelta(days=10)
Also, there would be no name conflict if you want to import datetime
later on.
此外,如果您希望稍后导入datetime,则不会出现名称冲突。
#8
2
If you want add days to date now, you can use this code
如果您想现在添加日期,您可以使用此代码
from datetime import datetime
from datetime import timedelta
date_now_more_5_days = (datetime.now() + timedelta(days=5) ).strftime('%Y-%m-%d')
#9
0
This is my initial but more complete development for a generic dateadd function in python
这是我对python中的通用dateadd函数的初步但更完整的开发
import datetime
from dateutil.relativedelta import relativedelta
def dateadd(date, part ,value):
if part=='year':
result = date + (value * relativedelta(years = 1))
elif part == 'month':
result = date + (value * relativedelta(months = 1))
elif part == 'day':
result = date + (value * relativedelta(days = 1))
elif part == 'hour':
result = date + (value * relativedelta(hours = 1))
elif part == 'minute':
result = date + (value * relativedelta(minutes = 1))
elif part == 'second':
result = date + (value * relativedelta(seconds = 1))
return result
date = datetime.datetime.now()
print(date, "now")
print(dateadd(date,'year',2) ,"year',2", )
print(dateadd(date,'year',-2) ,"year',-2", )
print(dateadd(date,'month',2) ,"month',2", )
print(dateadd(date,'month',-2) ,"month',-2", )
print(dateadd(date,'day',2) ,"day',2", )
print(dateadd(date,'day',-2) ,"day',-2", )
print(dateadd(date,'hour',2) ,"hour',2", )
print(dateadd(date,'hour',-2) ,"hour',-2", )
print(dateadd(date,'minute',2) ,"minute',2", )
print(dateadd(date,'minute',-2),"minute',-2", )
print(dateadd(date,'second',2) ,"second',2", )
print(dateadd(date,'second',-2),"second',-2", )
Result:
2018-03-08 09:04:12.619699 now
2020-03-08 09:04:12.619699 year',2
2016-03-08 09:04:12.619699 year',-2
2018-05-08 09:04:12.619699 month',2
2018-01-08 09:04:12.619699 month',-2
2018-03-10 09:04:12.619699 day',2
2018-03-06 09:04:12.619699 day',-2
2018-03-08 11:04:12.619699 hour',2
2018-03-08 07:04:12.619699 hour',-2
2018-03-08 09:06:12.619699 minute',2
2018-03-08 09:02:12.619699 minute',-2
2018-03-08 09:04:14.619699 second',2
2018-03-08 09:04:10.619699 second',-2
[参考译文]:,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,。