我在處理日期時發現非常有用的datetime.datetime對象,但是現在有情況,現在datime.datetime不適用於我。在程序執行期間,日期字段動態計算和這裏的問題:datetime.datetime - 日期字段超出範圍
>>> datetime.datetime(2013, 2, 29, 10, 15)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: day is out of range for month
OK,月沒有29日,但將是巨大的,如果日期時間能明白這一點,並返回該對象
datetime.datetime(2013, 3, 1, 10, 15)
什麼是最好的方式解決這種情況?所以,我在尋找一個通用的解決方案,當天參數大於月可能有的天數時。
不,這將*不*很大。顯式比隱式更好。 – 2013-02-28 11:31:12
您實際上無法獲得修復損壞的邏輯的一般解決方案。你可以嘗試使用'導入日曆; calendar.monthrange(year,month)'獲取月份日期。 – dmg 2013-02-28 11:36:51