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


Python datetime isoweekday()用法及代碼示例


Python datetime.isoweekday() 方法

datetime.isoweekday() 方法用於操作模塊 datetime 的 datetime 類的對象。

它使用一個 datetime 類對象並以整數形式返回星期幾,其中星期一是 1,星期日是 7。它與 datetime.weekday() 函數相同,我們認為星期一是 0,在接下來的幾天裏遞增 1。它是一個實例方法,因為它適用於日期類的實例。

模塊:

    import datetime

類:

    from datetime import datetime

用法:

    isoweekday()

參數:

  • None

返回值:

這個方法的返回類型是一個數字,它告訴我們那天是星期幾。

例:

## importing datetime class
from datetime import datetime

## Since we know the number, 
## we can save them in a list and 
## print the day on that number
day =["0","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

## Creating an instance
x = datetime.today() ## or use datetime.now()
d = x.isoweekday()
print("Today's isoweekday number is:", d)
print("Today's day is:",day[d])

x = datetime(2010, 1, 1)
print("Day on", x.year,"new year was:", day[x.isoweekday()])

x = datetime(2011, 1, 1)
print("Day on", x.year,"new year will be:", day[x.isoweekday()])

輸出

Today's isoweekday number is:5
Today's day is:Friday
Day on 2010 new year was:Friday
Day on 2011 new year will be:Saturday


相關用法


注:本文由純淨天空篩選整理自 Python datetime isoweekday() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。