📜  PythonDatetime.date类的Fromordinal()函数

📅  最后修改于: 2022-05-13 01:54:24.535000             🧑  作者: Mango

PythonDatetime.date类的Fromordinal()函数

fromordinal()函数用于返回指定的公历序号对应的公历日期。这与用于将公历日期转换为公历序数的 toordinal()函数相反。当负序数值或超出 date.max.toordinal() 返回值的序数传递给 toordinal()函数的参数时,此函数会引发 ValueError 。

实施例1:公历特定一天



Python3
# Python3 code for getting
# the Gregorian date corresponding
# to a given Gregorian ordinal.
 
# Importing datetime module
import datetime
 
# Specifying a Gregorian ordinal
ordinal = 123456;
 
# Calling the fromordinal() function
# over the specified Gregorian ordinal
date = datetime.date.fromordinal(ordinal);
 
# Printing the Gregorian date
print("The Gregorian date for the Gregorian\
ordinal %d is: %s"%(ordinal, date));


Python3
# Python3 code for getting
# the Gregorian date corresponding
# to a given Gregorian ordinal.
 
# Importing datetime module
import datetime
 
# Calling the fromordinal() function over
# the 1st day of Gregorian calendar as its parameter
date = datetime.date.fromordinal(1);
 
# Printing the Gregorian date for the 1st date
# of Gregorian calendar
print("Gregorian Date for the 1st day \
of Gregorian calendar: %s"%date);


输出:

The Gregorian date for the Gregorian ordinal 123456 is: 0339-01-05

示例 2:从公历的第一天开始

蟒蛇3

# Python3 code for getting
# the Gregorian date corresponding
# to a given Gregorian ordinal.
 
# Importing datetime module
import datetime
 
# Calling the fromordinal() function over
# the 1st day of Gregorian calendar as its parameter
date = datetime.date.fromordinal(1);
 
# Printing the Gregorian date for the 1st date
# of Gregorian calendar
print("Gregorian Date for the 1st day \
of Gregorian calendar: %s"%date);

输出:

Gregorian Date for the 1st day of Gregorian calendar: 0001-01-01