19

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 know which function can convert them back to YYYYMMDD format?

Patrick Yoder
  • 1,065
  • 4
  • 14
  • 19
James Bond
  • 7,533
  • 19
  • 50
  • 64

4 Answers4

20

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.

Peter Hansen
  • 21,046
  • 5
  • 50
  • 72
  • See also Toby Fernsler's answer. He mentions Rata Die and that probably explains what the OP was dealing with (though, unfortunately, the OP never clarified this). If the values are in fact Rata Die, my code above would produce incorrect results since Python's ordinal dates are not the same as Rata Die. – Peter Hansen May 08 '14 at 14:45
  • Why would you use `datetime.fromordinal()` instead of `date.fromordinal()`? We're only handling dates here, right? – MichielB Aug 30 '16 at 12:00
  • @MichielB, probably just habit, possibly from experience where some times when I tried using just date I found that I then wanted to do operations with it that only datetime allowed. Not entirely sure. – Peter Hansen Aug 30 '16 at 15:54
16

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.

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • Interesting, if that's the actual output. With Python (see my answer) it appears to be off by one. Obviously a dangerous way to represent dates (at least, without specifying the epoch). – Peter Hansen Apr 12 '10 at 15:29
  • What timezone are you in? It may be assuming the stamp is GMT and in the afternoon the date has already changed in Greenwitch. – Martin Beckett Apr 12 '10 at 15:31
  • 1
    @Peter: by one year and one day, you mean? – SilentGhost Apr 12 '10 at 15:33
  • @SilentGhost, yes! I obviously didn't notice the forest for the trees there. So *really* dangerous... – Peter Hansen Apr 12 '10 at 15:35
  • 2
    @Martin, that's a good thought, but I believe that since Python datetime objects are "naive" about timezones, by default, the values are effectively unaffected by my location relative to Greenwich/UTC. Anyway, SilentGhost's analysis invalidates the "off-by-one" theory. – Peter Hansen Apr 12 '10 at 15:42
6

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.

Toby Fernsler
  • 81
  • 1
  • 2
5

Is 733828.0 a timestamp? If so, you can do the following:

import datetime as dt
dt.date.fromtimestamp(733828.0).strftime('%Y%m%d')

PS

I think Peter Hansen is right :)

I am not a native English speaker. Just trying to help. I don't quite know the difference between a timestamp and an ordinal :(

satoru
  • 31,822
  • 31
  • 91
  • 141
  • The question states that those values correspond to "some date this year" so clearly they are not timestamps. The upvotes on this are badly placed... – Peter Hansen Apr 12 '10 at 15:31