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


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


Python date.weekday() 方法

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

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

模块:

    import datetime

类:

    from datetime import date

用法:

    weekday()

返回值:

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

例:

## importing date class
from datetime import date

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

x = date(2020, 10, 30)
d1 = x.weekday()
print("Weekday number on the date",x,"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," will be:", day[d1])

输出

Today's weekday number is:2
Weekday number on the date 2020-10-30 will be:4

Today's day is:Wednesday
Day on date 2020-10-30  will be:Friday


相关用法


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