Converting days since epoch to date
<p>How can one convert a serial date number, representing the number of days since epoch (1970), to the corresponding date string? I have seen multiple posts showing how to go from string to date number, but I haven't been able to find any posts on how to do the reverse.</p>

<p>For example, <code>15951</code> corresponds to <code>"2013-09-02"</code>.</p>

<pre><code>&gt;&gt;&gt; import datetime
&gt;&gt;&gt; (datetime.datetime(2013, 9, 2) - datetime.datetime(1970,1,1)).days + 1
15951
</code></pre>

<p>(The <code>+ 1</code> because whatever generated these date numbers followed the convention that Jan 1, 1970 = 1.)</p>

<p>TL;DR: Looking for something to do the following:</p>

<pre><code>&gt;&gt;&gt; serial_date_to_string(15951)  # arg is number of days since 1970
"2013-09-02"
</code></pre>

<p>This is different from <a href="http://stackoverflow.com/questions/12400256/python-converting-epoch-time-into-the-datetime">Python: Converting Epoch time into the datetime</a> because I am starting with days since 1970. I not sure if you can just multiply by 86,400 due to leap seconds, etc.</p>

<p>Use the <code>datetime</code> package as follows:</p>

<pre><code>import datetime
def serial_date_to_string(srl_no):
    new_date = datetime.datetime(1970,1,1,0,0) + datetime.timedelta(srl_no - 1)
    return new_date.strftime("%Y-%m-%d")
</code></pre>

<p>This is a function which returns the string as required.</p>

<p>So:</p>

<pre><code>serial_date_to_string(15951)
</code></pre>

<p>Returns</p>

<pre><code>&gt;&gt; "2013-09-02"
</code></pre>

python date datetime epoch