How to tell if a date is between two other dates in Python?
I have the following codes:
if date in (start, end):
print 'in between'
else:
print 'No!'
date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them. Thanks for any help!
python compare date
|
show 2 more comments
I have the following codes:
if date in (start, end):
print 'in between'
else:
print 'No!'
date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them. Thanks for any help!
python compare date
1
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about<
and>
in a tutorial yet?
– S.Lott
Mar 28 '11 at 20:03
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50
|
show 2 more comments
I have the following codes:
if date in (start, end):
print 'in between'
else:
print 'No!'
date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them. Thanks for any help!
python compare date
I have the following codes:
if date in (start, end):
print 'in between'
else:
print 'No!'
date, start and end are all variables with the format of 1/1. What should I do to have it print out the right result? i tried date as 10/2, start as 3/14 and end as 11/7 and it's print 'No!', which means it's not running right. I guess have to format them to a date format and then compare them. Thanks for any help!
python compare date
python compare date
asked Mar 28 '11 at 20:01
widgetwidget
3172719
3172719
1
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about<
and>
in a tutorial yet?
– S.Lott
Mar 28 '11 at 20:03
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50
|
show 2 more comments
1
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about<
and>
in a tutorial yet?
– S.Lott
Mar 28 '11 at 20:03
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50
1
1
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about
<
and >
in a tutorial yet?– S.Lott
Mar 28 '11 at 20:03
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about
<
and >
in a tutorial yet?– S.Lott
Mar 28 '11 at 20:03
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50
|
show 2 more comments
4 Answers
4
active
oldest
votes
As you are still not satisfied, I have another answer for you. Without using datetime and year.
It just uses built-in tuples and comparing them:
d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")
You can create tuple like these easily:
day = 16
month = 4
d = (month, day)
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
|
show 1 more comment
If you convert all your dates to datetime.date
, you can write the following:
if start <= date <= end:
print "in between"
else:
print "No!"
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
add a comment |
Use datetime.date
:
http://docs.python.org/library/datetime.html#datetime.date
<
operator is overloaded specially for you.
date1 < date2
- date1 is considered less than date2 when date1 precedes date2 in time.
>>> from datetime import date
>>> d1 = date(2011, 3, 28)
>>> d2 = date(2011, 3, 22)
>>> d3 = date(2011, 4, 3)
>>> d2 < d1 < d3
True
Or in your prgram:
from datetime import date
d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)
if d2 < d1 < d3:
print 'in between'
else:
print 'No!'
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
add a comment |
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2007', date_format)# Date to be checked
c = datetime.strptime('9/25/2008', date_format)
d = datetime.strptime('8/18/2008', date_format) #Date entered here should always be the same as 'a'
delta1 = b - a
delta2 = c - b
delta3 = d - a
if delta1.days >= delta3.days and delta2.days >= delta3.days:
print 'In between'
else:
print 'Not in between'
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',
autoActivateHeartbeat: false,
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%2f5464410%2fhow-to-tell-if-a-date-is-between-two-other-dates-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you are still not satisfied, I have another answer for you. Without using datetime and year.
It just uses built-in tuples and comparing them:
d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")
You can create tuple like these easily:
day = 16
month = 4
d = (month, day)
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
|
show 1 more comment
As you are still not satisfied, I have another answer for you. Without using datetime and year.
It just uses built-in tuples and comparing them:
d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")
You can create tuple like these easily:
day = 16
month = 4
d = (month, day)
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
|
show 1 more comment
As you are still not satisfied, I have another answer for you. Without using datetime and year.
It just uses built-in tuples and comparing them:
d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")
You can create tuple like these easily:
day = 16
month = 4
d = (month, day)
As you are still not satisfied, I have another answer for you. Without using datetime and year.
It just uses built-in tuples and comparing them:
d1 = (3, 28)
d2 = (3, 31)
d3 = (4, 2)
if d1 < d2 < d3:
print("BETWEEN!")
else:
print("NOT!")
You can create tuple like these easily:
day = 16
month = 4
d = (month, day)
edited Mar 28 '11 at 21:01
answered Mar 28 '11 at 20:56
Maciej ZiarkoMaciej Ziarko
5,962103764
5,962103764
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
|
show 1 more comment
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
I think this is exactly what i want. i have them converted to int and add to tuple. it works very well! Thank u so much!
– widget
Mar 28 '11 at 21:12
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
Just remember that doing it this way, you can't be sure if they are real dates. But if you're dealing with only real dates, it is probably the most efficient and easiest way.
– Maciej Ziarko
Mar 28 '11 at 21:18
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
I got the date data from my table. so they are real dates. yeah i like this comparing method, it's efficient
– widget
Mar 29 '11 at 17:09
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
Yeah, Python is a wonderful language. So logic and human.
– Maciej Ziarko
Mar 29 '11 at 17:52
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
@Ziarko: i agree!
– widget
Mar 30 '11 at 16:57
|
show 1 more comment
If you convert all your dates to datetime.date
, you can write the following:
if start <= date <= end:
print "in between"
else:
print "No!"
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
add a comment |
If you convert all your dates to datetime.date
, you can write the following:
if start <= date <= end:
print "in between"
else:
print "No!"
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
add a comment |
If you convert all your dates to datetime.date
, you can write the following:
if start <= date <= end:
print "in between"
else:
print "No!"
If you convert all your dates to datetime.date
, you can write the following:
if start <= date <= end:
print "in between"
else:
print "No!"
answered Mar 28 '11 at 20:05
Björn PollexBjörn Pollex
56.8k20156246
56.8k20156246
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
add a comment |
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
2
2
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Not sure how this isn't the selected answer, but thanks for it!
– Robert Grant
Jun 5 '15 at 11:33
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
Great you also added equality testing.
– RoadRunner
Jun 20 '18 at 6:18
add a comment |
Use datetime.date
:
http://docs.python.org/library/datetime.html#datetime.date
<
operator is overloaded specially for you.
date1 < date2
- date1 is considered less than date2 when date1 precedes date2 in time.
>>> from datetime import date
>>> d1 = date(2011, 3, 28)
>>> d2 = date(2011, 3, 22)
>>> d3 = date(2011, 4, 3)
>>> d2 < d1 < d3
True
Or in your prgram:
from datetime import date
d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)
if d2 < d1 < d3:
print 'in between'
else:
print 'No!'
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
add a comment |
Use datetime.date
:
http://docs.python.org/library/datetime.html#datetime.date
<
operator is overloaded specially for you.
date1 < date2
- date1 is considered less than date2 when date1 precedes date2 in time.
>>> from datetime import date
>>> d1 = date(2011, 3, 28)
>>> d2 = date(2011, 3, 22)
>>> d3 = date(2011, 4, 3)
>>> d2 < d1 < d3
True
Or in your prgram:
from datetime import date
d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)
if d2 < d1 < d3:
print 'in between'
else:
print 'No!'
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
add a comment |
Use datetime.date
:
http://docs.python.org/library/datetime.html#datetime.date
<
operator is overloaded specially for you.
date1 < date2
- date1 is considered less than date2 when date1 precedes date2 in time.
>>> from datetime import date
>>> d1 = date(2011, 3, 28)
>>> d2 = date(2011, 3, 22)
>>> d3 = date(2011, 4, 3)
>>> d2 < d1 < d3
True
Or in your prgram:
from datetime import date
d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)
if d2 < d1 < d3:
print 'in between'
else:
print 'No!'
Use datetime.date
:
http://docs.python.org/library/datetime.html#datetime.date
<
operator is overloaded specially for you.
date1 < date2
- date1 is considered less than date2 when date1 precedes date2 in time.
>>> from datetime import date
>>> d1 = date(2011, 3, 28)
>>> d2 = date(2011, 3, 22)
>>> d3 = date(2011, 4, 3)
>>> d2 < d1 < d3
True
Or in your prgram:
from datetime import date
d1 = date(2011, 3, 28)
d2 = date(2011, 3, 22)
d3 = date(2011, 4, 3)
if d2 < d1 < d3:
print 'in between'
else:
print 'No!'
edited Mar 28 '11 at 20:23
answered Mar 28 '11 at 20:05
Maciej ZiarkoMaciej Ziarko
5,962103764
5,962103764
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
add a comment |
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
your codes seem right to me. now i guess i would have to get records of year, month and date separately.i didnot think it is necessary though
– widget
Mar 28 '11 at 20:52
1
1
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I will write for you answer without year and without datetime. Just gime me 2 minutes.
– Maciej Ziarko
Mar 28 '11 at 20:53
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
I wrote another answer. Check it!
– Maciej Ziarko
Mar 28 '11 at 20:56
add a comment |
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2007', date_format)# Date to be checked
c = datetime.strptime('9/25/2008', date_format)
d = datetime.strptime('8/18/2008', date_format) #Date entered here should always be the same as 'a'
delta1 = b - a
delta2 = c - b
delta3 = d - a
if delta1.days >= delta3.days and delta2.days >= delta3.days:
print 'In between'
else:
print 'Not in between'
add a comment |
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2007', date_format)# Date to be checked
c = datetime.strptime('9/25/2008', date_format)
d = datetime.strptime('8/18/2008', date_format) #Date entered here should always be the same as 'a'
delta1 = b - a
delta2 = c - b
delta3 = d - a
if delta1.days >= delta3.days and delta2.days >= delta3.days:
print 'In between'
else:
print 'Not in between'
add a comment |
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2007', date_format)# Date to be checked
c = datetime.strptime('9/25/2008', date_format)
d = datetime.strptime('8/18/2008', date_format) #Date entered here should always be the same as 'a'
delta1 = b - a
delta2 = c - b
delta3 = d - a
if delta1.days >= delta3.days and delta2.days >= delta3.days:
print 'In between'
else:
print 'Not in between'
from datetime import datetime
date_format = "%m/%d/%Y"
a = datetime.strptime('8/18/2008', date_format)
b = datetime.strptime('9/26/2007', date_format)# Date to be checked
c = datetime.strptime('9/25/2008', date_format)
d = datetime.strptime('8/18/2008', date_format) #Date entered here should always be the same as 'a'
delta1 = b - a
delta2 = c - b
delta3 = d - a
if delta1.days >= delta3.days and delta2.days >= delta3.days:
print 'In between'
else:
print 'Not in between'
edited Aug 31 '16 at 4:56
answered Aug 12 '16 at 10:48
Vishrut JaipuriaVishrut Jaipuria
125
125
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.
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%2f5464410%2fhow-to-tell-if-a-date-is-between-two-other-dates-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
1
What else have you tried? Since that doesn't work, what other code do you think might work? Have you read about
<
and>
in a tutorial yet?– S.Lott
Mar 28 '11 at 20:03
i used start < date < end as well, and tried to use: from datetime import date. But it is still running wrong. this thing is that i just want to compare them in the month/date format since i have them in the same year. do i have to get the month and date out of month/date separately and then format them all together to compare?
– widget
Mar 28 '11 at 20:23
@widget: "still running wrong"? Please provide an actual example of what you mean by "running wrong". "do i have to get the month and date out of month/date separately". No.
– S.Lott
Mar 28 '11 at 20:36
Check the code in my answer. I tested it on both Python2.7 and Python3.1 and it works.
– Maciej Ziarko
Mar 28 '11 at 20:44
@S.Lott: in this case i have it running correctly: from datetime import date d1 = date(2010, 3, 14) d2 = date(2010, 10, 2) d3 = date(2010, 11, 7) if d2 < d1 < d3: print 'Yes!' else: print 'Wrong!' The thing is that i have to get records of year, month, date and put them in d1, d2, d3
– widget
Mar 28 '11 at 20:50