Is there a direct way to ignore parts of a python datetime object?
<p>I'm trying to compare two datetime objects, but ignoring the year. For example, given</p>

<pre><code>a = datetime.datetime(2015,07,04,01,01,01)
b = datetime.datetime(2016,07,04,01,01,01)
</code></pre>

<p>I want a == b to return True by ignoring the year. To do a comparison like this, I imagine I could just create new datetime objects with the same year like:</p>

<pre><code>c = datetime.datetime(2014,a.month,a.day,a.hour,a.minute,a.second)
d = datetime.datetime(2014,b.month,b.day,b.hour,b.minute,b.second)
</code></pre>

<p>However, this doesn't seem very pythonic. Is there a more direct method to do a comparison like what I'm asking?</p>

<p>I'm using python 3.4.</p>

<p>You can also consider comparing formatted date strings consisting of the fields you wish to include in the comparison. This allows you to be somewhat explicit while using shortened versions of the fields (as opposed to accessing <code>a.year</code>, <code>a.month</code>, etc.).</p>

<pre><code>from datetime import datetime

date_string = '%m %d %H %M %S'
a = datetime(2015, 7, 4, 1, 1, 1)
b = datetime(2016, 7, 4, 1, 1, 1)
print(a.strftime(date_string) == b.strftime(date_string))  # True
</code></pre>

python datetime