Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check item list from a range of dates with Python

I need to print all items of c that are in the range of a to b

01/08/2017, 02/08/2017, 03/08/2017, 04/08/2017, 05/08/2017, 06/08/2017, 07/08/2017, 08/08/2017, 09/08/2017, 10/08/2017, 11/08/2017, 12/08/2017, 13/08/2017, 14/08/2017, 15/08/2017, 16/08/2017, 17/08/2017, 18/08/2017, 19/08/2017, 20/08/2017, 21/08/2017, 22/08/2017, 23/08/2017, 24/08/2017, 25/08/2017, 26/08/2017, 27/08/2017, 28/08/2017

a = '01/08/2017'
b = '28/08/2017'
c = ['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017', '25/08/2020', '26/08/2020', '27/08/2020', '28/08/2020']

my answer should be:

['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017']
like image 810
Martin Bouhier Avatar asked Feb 19 '26 16:02

Martin Bouhier


1 Answers

You can use the datetime module:

import datetime
def to_datetime(d):
 day, month, year = map(int, d.split('/'))
 return datetime.datetime(year, month, day, 0, 0, 0)

a = '01/08/2017'
b = '28/08/2017'
_a = to_datetime(a)
_b = to_datetime(b)
c = ['01/08/2017', '20/08/2017', '21/08/2017', '22/08/2017', '23/08/2017', '24/08/2017', '25/08/2017', '26/08/2017', '27/08/2017', '28/08/2017']
for i in c:
  if _a <= to_datetime(i) <= _b:
    print(i)

Output:

01/08/2017
20/08/2017
21/08/2017
22/08/2017
23/08/2017
24/08/2017
25/08/2017
26/08/2017
27/08/2017
28/08/2017
like image 68
Ajax1234 Avatar answered Feb 21 '26 05:02

Ajax1234



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!