How to check if a date is between two dates in Python

2 Answers

0 votes
from datetime import datetime

d1 = '2023-02-01'
d =  '2023-02-03'
d2 = '2023-02-17'

print(datetime.strptime(d1, "%Y-%m-%d") <= datetime.strptime(d, "%Y-%m-%d") <= datetime.strptime(d2, "%Y-%m-%d"))




'''
run:

True

'''

 



answered Feb 23, 2023 by avibootz
0 votes
from datetime import datetime

d1 = '2023-02-01'
d =  '2023-02-03'
d2 = '2023-02-17'

print(d1 <= d <= d2)




'''
run:

True

'''

 



answered Feb 23, 2023 by avibootz
...