簡體   English   中英

Python檢查日期是否是未來30天

[英]Python check that the date is 30 days in the future

我想編寫一個測試,該測試從保存的對象recipient.expiry_date抽出時間,並檢查該日期是將來的30天。

這是我嘗試過的:

days_to_match = timezone.now() + timedelta(days=30)

self.assertEqual(recipient.expiry_date, days_to_match)

因為它還包含了時間,所以不會匹配。

如何才能做到這一點?

請注意, recipient.expiry_date用在我的模型設定timezone.now()這樣的比較對蟒蛇日期似乎錯誤。

正如您在該SO答案中看到的那樣,您可以使用date()僅比較日期而不是時間。 或者您可以將時間數據設為0。

self.assertEqual(recipient.expiry_date.date(), days_to_match.date())

您可以只查看日期部分:

self.assertEqual(recipient.expiry_date.year, days_to_match.year)
self.assertEqual(recipient.expiry_date.month, days_to_match.month)
self.assertEqual(recipient.expiry_date.day, days_to_match.day)

暫無
暫無

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

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