Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert the integer date format into YYYYMMDD?

Python and Matlab quite often have integer date representations as follows:

733828.0 733829.0 733832.0 733833.0 733834.0 733835.0 733836.0 733839.0 733840.0 733841.0

these numbers correspond to some dates this year. Do you guys know which function can convert them back to YYYYMMDD format?

thanks a million!

like image 300
James Bond Avatar asked Apr 12 '10 15:04

James Bond


People also ask

How do I convert a date field to Yyyymmdd in Excel?

Convert date to yyyy-mm-dd format with formula 1. Select a blank cell next to your date, for instance. I1, and type this formula =TEXT(G1, "yyyy-mm-dd"), and press Enter key, then drag AutoFill handle over the cells needed this formula. Now all dates are converted to texts and shown as yyyy-mm-dd format.


2 Answers

The datetime.datetime class can help you here. The following works, if those values are treated as integer days (you don't specify what they are).

>>> from datetime import datetime
>>> dt = datetime.fromordinal(733828)
>>> dt
datetime.datetime(2010, 2, 25, 0, 0)
>>> dt.strftime('%Y%m%d')
'20100225'

You show the values as floats, and the above doesn't take floats. If you can give more detail about what the data is (and where it comes from) it will be possible to give a more complete answer.

like image 106
Peter Hansen Avatar answered Oct 07 '22 01:10

Peter Hansen


Since Python example was already demonstrated, here is the matlab one:

>> datestr(733828, 'yyyymmdd')

ans =

20090224

Also, note that while looking similar these are actually different things in Matlab and Python:

Matlab
A serial date number represents the whole and fractional number of days from a specific date and time, where datenum('Jan-1-0000 00:00:00') returns the number 1. (The year 0000 is merely a reference point and is not intended to be interpreted as a real year in time.)

Python, datetime.date.fromordinal
Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1.

So they would differ by 366 days, which is apparently the length of the year 0.

like image 37
SilentGhost Avatar answered Oct 07 '22 01:10

SilentGhost