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


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


Python datetime.weekday() 方法

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

它使用 datetime 类对象并以整数形式返回星期几,其中星期一为 0,星期日为 6。它是一个实例方法,即它适用于类的实例。

模块:

    import datetime

类:

    from datetime import datetime

用法:

    weekday()

参数:

  • None

返回值:

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

例:

## importing datetime class
from datetime import datetime

## Creating an instance
x = datetime.today()
d = x.weekday()
print("Today's weekday number is:", d)

x = datetime(1996, 10,27, 21, 5, 5)
d1 = x.weekday()
xd = x.date()
print("Weekday number on the date", xd,"will be:",d1)
print()

## Since we know the number, 
## we can save them in a list and 
## print the day on that number
day =["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
print("Today's day is:",day[d])
print("Day on date", x," was:", day[d1])

输出

Today's weekday number is:5
Weekday number on the date 1996-10-27 will be:6

Today's day is:Saturday
Day on date 1996-10-27 21:05:05  was:Sunday


相关用法


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