簡體   English   中英

使用date.today()測試日期?

[英]Test dates using date.today()?

def test_dates(self):
    days = (date.today() + timedelta(days=2)) - date.today()
    print days
    if days >= -2 and days <= 4:
        print days

如何檢查兩個日期之間的差值在2-4天之間?

daysdatetime.timedelta對象。 您需要從中獲取days段。

if days.days >+ -2 and days.days < 4:

或更簡潔

if -2 <= days.days < 4:

在您的代碼中, days是一個timedelta對象。 使用days.days獲得整數天數。

使用days.days >= 2 and days.days <= 4可以

嘗試這種方式

if days.days >= -2 and days.days <= 4:
print days

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM