简体   繁体   中英

Check validity of datetime?

I would like to create a setter method for a datetime object so that I can customize the replace method. Is this possible?

For example:

datetime = datetime.replace(day = 34)

This throws an error because 34 is not a valid number of days for any month, but what I would like to do is increase the month by 1 and then subtract 28 , 30 , or 31 days.

this doesn't answer your question directly, but if you're just trying to increment the datetime object, you can use timedelta. For example:

from datetime import datetime
from datetime import timedelta

datetime(2011, 1, 1) + timedelta(days=34)

您可以继承datetime并提供自己的replace方法实现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM