@TOC
以前写的存一个,难得找。
def date_count(self, date, deltaDay):
'''
该函数实现日期的增减,粗暴法,直接给多少差值计算多少差值,暂时只支持最多28天加减
:param date: 当前日期
:param delta: 差值日期
:return:
算法:分闰年,分日期
'''
datelist = date.split('-')
year = datelist[0]
month = datelist[1]
day = datelist[2]
int_year = self.str2int(year)
int_month = self.str2int(month)
int_day = self.str2int(day)
this_month_day_count = self.getMonthDayCount(int_year, int_month) # 获取本月日期数
last_month = self.getLastMonth(int_month) # 获取上一个月份
next_month = self.getNextMonth(int_month) # 获取下一个月份
result_day = int_day + deltaDay # 计算后的日数
if result_day > this_month_day_count: # 跨下月 当该数大于本月最大日期后,转到下一月,假如是6月28,加7天,28+7=35,35-30=5,就是7月5,注意跨年
if int_month > next_month: # 当本月大于下月时,说明本月是12月,跨年
next_int_year = int_year + 1
new_day = result_day - this_month_day_count
# newDate = str(next_int_year) +'-'+self.int2str(next_month)+'-'+self.int2str(new_day)
newDate = str(next_int_year) + '-01-' + self.int2str(new_day)
return newDate
else: # 不跨年
new_day = result_day - this_month_day_count
newDate = year + '-' + self.int2str(next_month) + '-' + self.int2str(new_day)
return newDate
elif result_day < 1: # 跳上月 当本来就是月初时,这会跳到上一月,假如是6月02,减7天,2-7=-5,上月31天,31-5=26,就是5月26
if last_month > int_month: # 当上月大于本月时,说明本月是1月上月12 ,跨年
last_int_year = int_year - 1
new_day = 31 + result_day
newDate = str(last_int_year) + '-12-' + self.int2str(new_day)
# print('跨年,上一月')
return newDate
else:
last_month_endDay = self.getLastMonthDay(int_year, int_month)
new_day = last_month_endDay + result_day
newDate = year + '-' + self.int2str(last_month) + '-' + self.int2str(new_day)
# print('不跨年,上一月')
return newDate
else: # 本月
newDate = year + '-' + month + '-' + self.int2str(result_day)
return newDate