Adding 5 days to a date in Python
up vote
261
down vote
favorite
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.
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'
Now I want to add 5 days to this date. I used the following code:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
name 'timedelta' is not defined
python date datetime
add a comment |
up vote
261
down vote
favorite
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.
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'
Now I want to add 5 days to this date. I used the following code:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
name 'timedelta' is not defined
python date datetime
8
General clue: if you get the errorname 'timedelta' is not defined
, that means that you haven't definedtimedelta
anywhere. Python is usually pretty informative about its error messages.
– Katriel
Jul 29 '11 at 10:06
1
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
1
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
5
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53
add a comment |
up vote
261
down vote
favorite
up vote
261
down vote
favorite
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.
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'
Now I want to add 5 days to this date. I used the following code:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
name 'timedelta' is not defined
python date datetime
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.
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'
Now I want to add 5 days to this date. I used the following code:
EndDate = Date.today()+timedelta(days=10)
Which returned this error:
name 'timedelta' is not defined
python date datetime
python date datetime
edited Jul 11 '16 at 12:09
gary
3,80022348
3,80022348
asked Jul 29 '11 at 9:17
MuraliKrishna
1,50131211
1,50131211
8
General clue: if you get the errorname 'timedelta' is not defined
, that means that you haven't definedtimedelta
anywhere. Python is usually pretty informative about its error messages.
– Katriel
Jul 29 '11 at 10:06
1
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
1
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
5
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53
add a comment |
8
General clue: if you get the errorname 'timedelta' is not defined
, that means that you haven't definedtimedelta
anywhere. Python is usually pretty informative about its error messages.
– Katriel
Jul 29 '11 at 10:06
1
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
1
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
5
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53
8
8
General clue: if you get the error
name 'timedelta' is not defined
, that means that you haven't defined timedelta
anywhere. Python is usually pretty informative about its error messages.– Katriel
Jul 29 '11 at 10:06
General clue: if you get the error
name 'timedelta' is not defined
, that means that you haven't defined timedelta
anywhere. Python is usually pretty informative about its error messages.– Katriel
Jul 29 '11 at 10:06
1
1
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
1
1
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
5
5
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53
add a comment |
8 Answers
8
active
oldest
votes
up vote
399
down vote
accepted
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta
:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
@paulmorriss: You are calling thestrptime
method on thedatetime
class in thedatetime
module, so you need to specifydatetime.datetime
.
– Graeme Perrow
Jan 6 '15 at 22:43
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What isdatetime
? You can't rely on convention to know, but always have to look at the imports.
– Xiong Chiamiov
Jun 5 '17 at 17:47
5
Long tail legacy problem there. it "should" befrom datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.
– Aaron McMillin
Jun 26 '17 at 20:28
|
show 4 more comments
up vote
94
down vote
Import timedelta
first.
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
EndDate = Date + timedelta(days=10)
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
@dan-klasson It doesn't work for me,date
object don't havetimedelta
method. What Python version are you using?
– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
add a comment |
up vote
12
down vote
I guess you are missing something like that:
from datetime import timedelta
add a comment |
up vote
10
down vote
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)
add a comment |
up vote
10
down vote
Here is another method to add days on date using dateutil's relativedelta.
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
After 5 Days: 30/06/2015 15:56:09
add a comment |
up vote
8
down vote
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
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
add a comment |
up vote
4
down vote
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.
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.
add a comment |
up vote
3
down vote
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')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6871016%2fadding-5-days-to-a-date-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
399
down vote
accepted
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta
:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
@paulmorriss: You are calling thestrptime
method on thedatetime
class in thedatetime
module, so you need to specifydatetime.datetime
.
– Graeme Perrow
Jan 6 '15 at 22:43
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What isdatetime
? You can't rely on convention to know, but always have to look at the imports.
– Xiong Chiamiov
Jun 5 '17 at 17:47
5
Long tail legacy problem there. it "should" befrom datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.
– Aaron McMillin
Jun 26 '17 at 20:28
|
show 4 more comments
up vote
399
down vote
accepted
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta
:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
@paulmorriss: You are calling thestrptime
method on thedatetime
class in thedatetime
module, so you need to specifydatetime.datetime
.
– Graeme Perrow
Jan 6 '15 at 22:43
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What isdatetime
? You can't rely on convention to know, but always have to look at the imports.
– Xiong Chiamiov
Jun 5 '17 at 17:47
5
Long tail legacy problem there. it "should" befrom datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.
– Aaron McMillin
Jun 26 '17 at 20:28
|
show 4 more comments
up vote
399
down vote
accepted
up vote
399
down vote
accepted
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta
:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
The previous answers are correct but it's generally a better practice to do:
import datetime
Then you'll have, using datetime.timedelta
:
date_1 = datetime.datetime.strptime(start_date, "%m/%d/%y")
end_date = date_1 + datetime.timedelta(days=10)
edited Sep 25 '15 at 9:43
Mathieu Rodic
4,59522939
4,59522939
answered Jul 29 '11 at 10:03
Botond Béres
9,53222539
9,53222539
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
@paulmorriss: You are calling thestrptime
method on thedatetime
class in thedatetime
module, so you need to specifydatetime.datetime
.
– Graeme Perrow
Jan 6 '15 at 22:43
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What isdatetime
? You can't rely on convention to know, but always have to look at the imports.
– Xiong Chiamiov
Jun 5 '17 at 17:47
5
Long tail legacy problem there. it "should" befrom datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.
– Aaron McMillin
Jun 26 '17 at 20:28
|
show 4 more comments
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
@paulmorriss: You are calling thestrptime
method on thedatetime
class in thedatetime
module, so you need to specifydatetime.datetime
.
– Graeme Perrow
Jan 6 '15 at 22:43
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What isdatetime
? You can't rely on convention to know, but always have to look at the imports.
– Xiong Chiamiov
Jun 5 '17 at 17:47
5
Long tail legacy problem there. it "should" befrom datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.
– Aaron McMillin
Jun 26 '17 at 20:28
4
4
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
datetime.datetime - why twice?
– paulmorriss
Aug 11 '14 at 9:11
31
31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
importing like "from datetime import datetime, timedelta" would add readibility to the code
– Manel Clos
Nov 12 '14 at 13:31
5
5
@paulmorriss: You are calling the
strptime
method on the datetime
class in the datetime
module, so you need to specify datetime.datetime
.– Graeme Perrow
Jan 6 '15 at 22:43
@paulmorriss: You are calling the
strptime
method on the datetime
class in the datetime
module, so you need to specify datetime.datetime
.– Graeme Perrow
Jan 6 '15 at 22:43
1
1
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is
datetime
? You can't rely on convention to know, but always have to look at the imports.– Xiong Chiamiov
Jun 5 '17 at 17:47
Can we all agree that naming a commonly-used class the same name as the module containing it is a dumb idea? What is
datetime
? You can't rely on convention to know, but always have to look at the imports.– Xiong Chiamiov
Jun 5 '17 at 17:47
5
5
Long tail legacy problem there. it "should" be
from datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.– Aaron McMillin
Jun 26 '17 at 20:28
Long tail legacy problem there. it "should" be
from datetime import DateTime
since classes are CamelCased, but datetime precedes PEP8.– Aaron McMillin
Jun 26 '17 at 20:28
|
show 4 more comments
up vote
94
down vote
Import timedelta
first.
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
EndDate = Date + timedelta(days=10)
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
@dan-klasson It doesn't work for me,date
object don't havetimedelta
method. What Python version are you using?
– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
add a comment |
up vote
94
down vote
Import timedelta
first.
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
EndDate = Date + timedelta(days=10)
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
@dan-klasson It doesn't work for me,date
object don't havetimedelta
method. What Python version are you using?
– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
add a comment |
up vote
94
down vote
up vote
94
down vote
Import timedelta
first.
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
EndDate = Date + timedelta(days=10)
Import timedelta
first.
from datetime import timedelta
And Date.today()
will return today's datetime, may be you want
EndDate = Date + timedelta(days=10)
answered Jul 29 '11 at 9:20
DrTyrsa
22.9k66379
22.9k66379
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
@dan-klasson It doesn't work for me,date
object don't havetimedelta
method. What Python version are you using?
– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
add a comment |
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
@dan-klasson It doesn't work for me,date
object don't havetimedelta
method. What Python version are you using?
– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
10
10
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
datetime.date.today() instead of Date.today()
– elsadek
Aug 6 '14 at 15:19
1
1
@dan-klasson It doesn't work for me,
date
object don't have timedelta
method. What Python version are you using?– DrTyrsa
Jul 17 '17 at 6:32
@dan-klasson It doesn't work for me,
date
object don't have timedelta
method. What Python version are you using?– DrTyrsa
Jul 17 '17 at 6:32
@DrTyrsa My bad. Should be:
from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
@DrTyrsa My bad. Should be:
from datetime import timedelta, date; date.today() + timedelta(days=10)
– dan-klasson
Jul 17 '17 at 7:07
add a comment |
up vote
12
down vote
I guess you are missing something like that:
from datetime import timedelta
add a comment |
up vote
12
down vote
I guess you are missing something like that:
from datetime import timedelta
add a comment |
up vote
12
down vote
up vote
12
down vote
I guess you are missing something like that:
from datetime import timedelta
I guess you are missing something like that:
from datetime import timedelta
answered Jul 29 '11 at 9:20
vstm
10.6k13742
10.6k13742
add a comment |
add a comment |
up vote
10
down vote
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)
add a comment |
up vote
10
down vote
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)
add a comment |
up vote
10
down vote
up vote
10
down vote
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)
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)
answered Aug 29 '14 at 14:10
fantabolous
7,60032836
7,60032836
add a comment |
add a comment |
up vote
10
down vote
Here is another method to add days on date using dateutil's relativedelta.
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
After 5 Days: 30/06/2015 15:56:09
add a comment |
up vote
10
down vote
Here is another method to add days on date using dateutil's relativedelta.
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
After 5 Days: 30/06/2015 15:56:09
add a comment |
up vote
10
down vote
up vote
10
down vote
Here is another method to add days on date using dateutil's relativedelta.
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
After 5 Days: 30/06/2015 15:56:09
Here is another method to add days on date using dateutil's relativedelta.
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
After 5 Days: 30/06/2015 15:56:09
answered Jun 25 '15 at 12:56
Atul Arvind
9,82153449
9,82153449
add a comment |
add a comment |
up vote
8
down vote
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
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
add a comment |
up vote
8
down vote
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
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
add a comment |
up vote
8
down vote
up vote
8
down vote
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
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
edited Nov 14 '14 at 8:17
answered Nov 14 '14 at 8:01
Guray Celik
76111011
76111011
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
add a comment |
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
1
1
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
Good code. But your IF to test the addDays in get_date is not necessary
– ECC
Nov 24 '14 at 15:09
add a comment |
up vote
4
down vote
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.
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.
add a comment |
up vote
4
down vote
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.
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.
add a comment |
up vote
4
down vote
up vote
4
down vote
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.
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.
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.
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.
answered Sep 27 '17 at 10:42
toto_tico
7,36234259
7,36234259
add a comment |
add a comment |
up vote
3
down vote
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')
add a comment |
up vote
3
down vote
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')
add a comment |
up vote
3
down vote
up vote
3
down vote
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')
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')
answered Feb 20 at 19:51
Jorge Omar Martinez
14124
14124
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f6871016%2fadding-5-days-to-a-date-in-python%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
8
General clue: if you get the error
name 'timedelta' is not defined
, that means that you haven't definedtimedelta
anywhere. Python is usually pretty informative about its error messages.– Katriel
Jul 29 '11 at 10:06
1
Search didn't work? All of these code examples would have helped: stackoverflow.com/search?q=python+timedelta. There appear to be over 200 questions just like this one.
– S.Lott
Jul 29 '11 at 10:08
1
possible duplicate of add days to a date in Python using loops, ranges, and slicing
– S.Lott
Jul 29 '11 at 10:11
5
You want to add five days, but then you have timedelta(days=10)…I'm confused about where the 10 came from and why it isn't 5
– FeifanZ
Jan 7 '13 at 0:53