15

In python, a dateobject can be converted in the proleptic Gregorian ordinal this way:

d=datetime.date(year=2010, month=3, day=1)
d.toordinal()

but what is the reverse operation?

Mermoz
  • 14,898
  • 17
  • 60
  • 85

3 Answers3

12

The opposite is date.fromordinal

classmethod date.fromordinal(ordinal)

    Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. ValueError is raised unless 1 <= ordinal <= date.max.toordinal(). For any date d, date.fromordinal(d.toordinal()) == d.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
5

It's date.fromordial() as Jon wrote in the comments.

or datetime.fromordinal()

You can read more about it in the date= documentation

and for datetime

From the docs:

classmethod date.fromordinal(ordinal)

Return the date corresponding to the proleptic Gregorian ordinal, where January 1 of year 1 has ordinal 1. ValueError is raised unless 1 <= ordinal <= date.max.toordinal().

For any date d, date.fromordinal(d.toordinal()) == d.

Ewan
  • 14,592
  • 6
  • 48
  • 62
4

I found the answer in this question.

>>> from datetime import datetime
>>> dt = datetime.fromordinal(733828)
Community
  • 1
  • 1
Mermoz
  • 14,898
  • 17
  • 60
  • 85