How to check how many emails were sent between dates?
Question: I want to count how many emails were sent between certain dates. The date
header looks like this:

    
    
    Date: Tue, 20 Jan 2015 15:00:37 +0000

When counting other things, I code like this which adds one to the count:

    
    
    if msg['From'] == 'example@example.com':
     count+=1
    

However it gets difficult when I have to count how many emails were sent
between certain dates. Would it be something like:

date1= Tue, 18 Jan 2015 15:00:37 date2= Wed, 23 Jan 2015 15:00:37

    
    
    if msg['Date'] < date2 and msg['Date'] > date1:
     count+=1
    

I Don't think python will recognize it being dates. Therefore the `>` or `<`
operators will not work?


Answer: You can use dateutil or datetime to parse the strings as datetime objects and
compare:

    
    
    from dateutil import parser
    
    date1= "Tue, 18 Jan 2015 15:00:37"
    date2="Wed, 23 Jan 2015 15:00:37"
    date1 = parser.parse(date1)
    date2 = parser.parse(date2)
    msg = {"Date": "Tue, 19 Jan 2015 15:00:37 +0000"}
    if date2 >= parser.parse(msg['Date']).replace(tzinfo=None)  > date1:
         print msg['Date']
    

You will need to `pip install python-dateutil` as it is not in the standard
lib.

Your Date string seems to include the timezone but date1 and 2 don't so you
cannot compare offset-aware and offset-naive datetimes.

