In python, a date
object 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?
toordinal() is a simple method used to manipulate the objects of DateTime class. It returns proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. The function returns the ordinal value for the given DateTime object.
The date class is used to instantiate date objects in Python. When an object of this class is instantiated, it represents a date in the format YYYY-MM-DD. Constructor of this class needs three mandatory arguments year, month and date.
You just forgot to use %d in order to capture the date number and the : for the time and you ALSO need to capture +0000 . Show activity on this post.
Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.
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.
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 unless1 <= ordinal <= date.max.toordinal()
.For any date
d
,date.fromordinal(d.toordinal()) == d.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With