當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。