Converting and comparing 2 datetimes
<p>I need help trying to convert a string to datetime, then comparing it to see if it is less than 3 days old. I have tried both with the time class, as well as the datetime class, but I keep getting the same error:</p>

<pre><code>TypeError: unsupported operand type(s) for -: 'str' and 'str'
</code></pre>

<p>here is the code I have tried:</p>

<pre><code>def time_calculation():
    time1 = "2:00 PM 5 Oct 2016"
    time2 = "2:00 PM 4 Oct 2016"
    time3 = "2:00 PM 1 Oct 2016"
    timeNow = time.strftime("%Y%m%d-%H%M%S")
    #newtime1 = time.strftime("%Y%m%d-%H%M%S", time.strptime(time1, "%I:%M %p %d %b %Y"))
    newtime1 = datetime.strptime(time1, "%I:%M %p %d %b %Y").strftime("%Y%m%d-%H%M%S")
    print("the new time1 is {}".format(newtime1))
    #newtime2 = time.strftime("%Y%m%d-%H%M%S", time.strptime(time2, "%I:%M %p %d %b %Y"))
    newtime2 = datetime.strptime(time2, "%I:%M %p %d %b %Y").strftime("%Y%m%d-%H%M%S")
    print("the new time2 is {}".format(newtime2))
    #newtime3 = time.strftime("%Y%m%d-%H%M%S", time.strptime(time3, "%I:%M %p %d %b %Y"))
    newtime3 = datetime.strptime(time3, "%I:%M %p %d %b %Y").strftime("%Y%m%d-%H%M%S")
    print("the new time3 is {}".format(newtime3))
    timeList = []
    timeList.append(newtime1)
    timeList.append(newtime2)
    timeList.append(newtime3)

    for ele in timeList:
        deltaTime = ele - timeNow
        if deltaTime.days &lt; 4:
            print("This time was less than 4 days old {}\n".format(ele.strftime("%Y%m%d-%H%M%S")))
</code></pre>

<p>the commented out parts are what I did with time, while the others are with datetime. </p>

<p>the error happens at the line where I try to compare the current time with each time in the list, but it takes them as strings instead of datetimes and won't subtract them so I can compare them. (In the for loop at the bottom.)</p>

<p>Indeed, don't convert back to strings and instead work with <code>datetime</code> objects. As noted in the error message <code>str - str</code> isn't an operation that's defined (what does it mean to subtract a string from another?):</p>

<pre><code>"s" - "s" # TypeError
</code></pre>

<p>Instead, initialize <code>timeNow</code> with <code>datetime.now()</code>, <code>datetime</code> instances support subtracting. As a second suggestion, subtract <code>ele</code> from <code>timeNow</code> and not <code>timeNow</code> from <code>ele</code>: </p>

<pre><code>def time_calculation():
    # snipped for brevity
    timeNow = datetime.now()

    # snipped

    for ele in timeList:
        deltaTime = timeNow - ele
        if deltaTime.days &lt; 4:
            print("This time was less than 4 days old {}\n".format(ele.strftime("%Y%m%d-%H%M%S")))
</code></pre>

<p>Prints out:</p>

<pre><code>time_calculation()
the new time1 is 2016-10-05 14:00:00
the new time2 is 2016-10-04 14:00:00
the new time3 is 2016-10-01 14:00:00
This time was less than 4 days old 20161005-140000

This time was less than 4 days old 20161004-140000
</code></pre>

<p>Which I'm guessing is what you were after.</p>

python python-3.x datetime