9

How to check if a date is between two dates in python?

EDIT1: In excel, the date that I am reading is 7/13/2018 for example.

Mat.S
  • 1,814
  • 7
  • 24
  • 36
  • Give example of date format that you are using. – Nabin Jul 19 '17 at 04:15
  • Please see the update – Mat.S Jul 19 '17 at 04:16
  • Duplicate of [How to tell if a date is between two other dates in Python?](https://stackoverflow.com/questions/5464410/how-to-tell-if-a-date-is-between-two-other-dates-in-python). – miradulo Jul 19 '17 at 04:20
  • 3
    Possible duplicate of [How to tell if a date is between two other dates in Python?](https://stackoverflow.com/questions/5464410/how-to-tell-if-a-date-is-between-two-other-dates-in-python) – Jacobm001 Jul 19 '17 at 04:21

1 Answers1

11

You can convert the date so it can be easily compared in Python. Dates 1 and 2 are the converted dates, input_1 and input_2 are the two inputs from Excel. The second parameter passed in is the format in which the date is passed in.

date_1 = datetime.strptime(input_1, '%d/%m/%Y')
date_2 = datetime.strptime(input_2, '%d/%m/%Y')
date_3 = date(2017, 7, 19)

Now that you have the dates in the right format, I will check if date_3 falls within date_1 and date_2. This will return a Boolean.

date_1 < date_3 < date_2
PeskyPotato
  • 670
  • 8
  • 20