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


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