当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python datetime isoweekday()用法及代码示例


Python datetime.isoweekday() 方法

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

它使用一个 datetime 类对象并以整数形式返回星期几,其中星期一是 1,星期日是 7。它与 datetime.weekday() 函数相同,我们认为星期一是 0,在接下来的几天里递增 1。它是一个实例方法,因为它适用于日期类的实例。

模块:

    import datetime

类:

    from datetime import datetime

用法:

    isoweekday()

参数:

  • None

返回值:

这个方法的返回类型是一个数字,它告诉我们那天是星期几。

例:

## importing datetime class
from datetime import datetime

## Since we know the number, 
## we can save them in a list and 
## print the day on that number
day =["0","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

## Creating an instance
x = datetime.today() ## or use datetime.now()
d = x.isoweekday()
print("Today's isoweekday number is:", d)
print("Today's day is:",day[d])

x = datetime(2010, 1, 1)
print("Day on", x.year,"new year was:", day[x.isoweekday()])

x = datetime(2011, 1, 1)
print("Day on", x.year,"new year will be:", day[x.isoweekday()])

输出

Today's isoweekday number is:5
Today's day is:Friday
Day on 2010 new year was:Friday
Day on 2011 new year will be:Saturday


相关用法


注:本文由纯净天空筛选整理自 Python datetime isoweekday() Method with Example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。