What is date.fromordinal(ordinal) in Python?

Overview

The fromordinal() method is used to convert a Gregorian ordinal to an equivalent Gregorian date format. It is a reverse method of toordinal() that takes a Gregorian date as an argument and returns an ordinal.

Syntax

date.fromordinal(ordinal)

Parameters

ordinal: It takes an ordinal as an argument value.

Return value

It returns the date in Gregorian calendar format.

Example

# Load datetime module
import datetime
# Random ordinal
ordinal = 2141;
# passing Gregorian ordinal to fromordinal() method
date = datetime.date.fromordinal(ordinal);
# Printing the Gregorian date
print("Date Parallel to Gregorian Ordinal %d is: %s"
%(ordinal, date))

Explanation

  • Line 2: We import the datetime library.
  • Line 4: We declare an ordinal variable and assign a value to it.
  • Line 6: We call the date.fromordinal method.
  • Line 9: We print the Gregorian date.

Free Resources