带有示例的Python datetime toordinal()方法

Python datetime.toordinal()方法 (Python datetime.toordinal() Method)

datetime.toordinal() method is used to manipulate objects of datetime class of module datetime.

datetime.toordinal()方法用于操作模块datetime的datetime类的对象。

It is used to return the proleptic Gregorian ordinal of the date, where January 1 of year 1 has ordinal 1. If January 1 of year 1 has ordinal number 1 then January 2 year 1 will have ordinal number 2 and so on. It is an instance method which means that it works on an instance of the class.

它用于返回日期的多边格里高利度序数,其中1年1月1日具有序数1。如果1年1月1日具有序数1,则1年1月2日将具有序数2,依此类推。 这是一个实例方法,这意味着它可以在类的实例上工作。

Module:

模块:

    import datetime

Class:

类:

    from datetime import datetime

Syntax:

句法:

    toordinal()

Parameter(s):

参数:

  • None

    没有

Return value:

返回值:

The return type of this method is a number which is the ordinal number of that date in Gregorian calendar.

此方法的返回类型是一个数字,它是该日期在公历中的序数。

Example:

例:

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime(2020,10,27, 21, 5,27, 100)
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

## Using today' date
x = datetime.now()
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

x = datetime.today()
d = x.toordinal()
print("Ordinal number of date ",x," is:", d)
print()

x = datetime(1, 1, 1, 0,0,0,0)
d = x.toordinal()
print("Ordinal number of the earliest possible date allowed",x," is:", d)
print()

x = datetime(9999, 12, 31, 23, 59, 59)
d = x.toordinal()
print("Ordinal number of the largest maximum date allowed",x," is:", d)
print()

Output

输出量

Ordinal number of date  2020-10-27 21:05:27.000100  is: 737725

Ordinal number of date  2020-05-02 06:50:06.336005  is: 737547

Ordinal number of date  2020-05-02 06:50:06.336068  is: 737547

Ordinal number of the earliest possible date allowed 0001-01-01 00:00:00  is: 1

Ordinal number of the largest maximum date allowed 9999-12-31 23:59:59  is: 3652059

Note: Attribute of the datetime objects should be in the given range otherwise it will show a ValueError.

注意: datetime对象的属性应在给定范围内,否则将显示ValueError

Example:

例:

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime(99999, 1, 31)
d = x.toordinal()
print("Ordinal number of the date",x," is:", d)
print()

Output

输出量

Traceback (most recent call last):
  File "main.py", line 5, in <module>
    x = datetime(99999, 1, 31)
ValueError: year 99999 is out of range


翻译自: https://www.includehelp.com/python/datetime-toordinal-method-with-example.aspx

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值