How to convert the integer date format into YYYYMMDD? How to convert the integer date format into YYYYMMDD? python python

How to convert the integer date format into YYYYMMDD?


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)>>> dtdatetime.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.


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.


Dates like 733828.0 are Rata Die dates, counted from January 1, 1 A.D. (and decimal fraction of days). They may be UTC or by your timezone.

Julian Dates, used mostly by astronomers, count the days (and decimal fraction of days) since January 1, 4713 BC Greenwich noon. Julian date is frequently confused with Ordinal date, which is the date count from January 1 of the current year (Feb 2 = ordinal day 33).

So datetime is calling these things ordinal dates, but I think this only makes sense locally, in the world of python.