Python中可以使用datetime
模块来实现日期的加减功能。具体步骤如下:
datetime
模块from datetime import datetime, timedelta
date
date = datetime(year, month, day)
timedelta
来加减日期# 加一天
new_date = date + timedelta(days=1)
# 减一天
new_date = date - timedelta(days=1)
# 加一周
new_date = date + timedelta(weeks=1)
# 减一周
new_date = date - timedelta(weeks=1)
完整示例代码如下:
from datetime import datetime, timedelta
date = datetime(2022, 1, 1)
new_date = date + timedelta(days=1)
print(new_date)
new_date = date - timedelta(days=1)
print(new_date)
new_date = date + timedelta(weeks=1)
print(new_date)
new_date = date - timedelta(weeks=1)
print(new_date)
这样就可以实现对日期进行加减的功能了。