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'll need to use <a href="https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime" rel="nofollow"><code>strptime</code></a> on the date string, specifying the format, then you can call the <code>toordinal</code> method of the <code>date</code> object:</p>

<pre><code>&gt;&gt;&gt; from datetime import datetime as dt
&gt;&gt;&gt; d = dt.strptime('2010-03-01', '%Y-%m-%d').date()
&gt;&gt;&gt; d
datetime.date(2010, 3, 1)
&gt;&gt;&gt; d.toordinal()
733832
</code></pre>

<p>The call to the <code>date</code> method in this case is redundant, and is only kept for making the object consistent as a <code>date</code> object instead of a <code>datetime</code> object.</p>

<p>If you're looking to handle more date string formats, <a href="http://strftime.org/" rel="nofollow">Python's strftime directives</a> is one good reference you want to check out.</p>

python datetime