Convert date to ordinal python?
<p>I want to convert
2010-03-01 to 733832</p>

<p>I just found this toordinal code</p>

<pre><code>d=datetime.date(year=2010, month=3, day=1)
d.toordinal()
</code></pre>

<p>from <a href="http://stackoverflow.com/questions/16542074/what-is-the-inverse-of-date-toordinal-in-python">this</a></p>

<p>But i want something more like</p>

<pre><code>d=datetime.date('2010-03-01')
d.toordinal()
</code></pre>

<p>Thanks in advance</p>

<p>You need to firstly convert the time string to <code>datetime</code> object using <a href="https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime" rel="nofollow">strptime()</a>. Then call <code>.toordinal()</code> on the <code>datetime</code> object</p>

<pre><code>&gt;&gt;&gt; from datetime import datetime
&gt;&gt;&gt; date = datetime.strptime('2010-03-01', '%Y-%M-%d')
&gt;&gt;&gt; date.toordinal()
733773
</code></pre>

<p>You should be creating the function to achieve this as:</p>

<pre><code>def convert_date_to_ordinal(date):
    return datetime.strptime(date, '%Y-%M-%d').toordinal()

convert_date_to_ordinal('2010-03-01')
#returns: 733773
</code></pre>

python datetime