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 astimezone()用法及代碼示例
- Python datetime timetuple()用法及代碼示例
- Python datetime timetz()用法及代碼示例
- Python datetime isocalendar()用法及代碼示例
- Python datetime date()用法及代碼示例
- Python datetime isoformat()用法及代碼示例
- Python datetime __str__()用法及代碼示例
- Python datetime time()用法及代碼示例
- Python datetime utcoffset()用法及代碼示例
- Python datetime tzname()用法及代碼示例
- Python datetime replace()用法及代碼示例
- Python datetime strftime()用法及代碼示例
- Python datetime toordinal()用法及代碼示例
- Python datetime isoweekday()用法及代碼示例
- Python datetime.timedelta()用法及代碼示例
- Python datetime.tzinfo()用法及代碼示例
- Python date toordinal()用法及代碼示例
- Python date replace()用法及代碼示例
- Python date strftime()用法及代碼示例
- Python date weekday()用法及代碼示例
注:本文由純淨天空篩選整理自 Python datetime weekday() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。