最近转战金额业务,用sosotest进行接口自动化,需要使用python,涉及日期时间处理的一些好用的方法封装
#获取前后n个月的日期,前一个月monthValue=-1,后一个月monthValue=1
def getBeforeAfterMonthDate(dateStr, monthValue):
fmt = '%Y%m%d'
time_tuple = time.strptime(dateStr, fmt)
year, month, day = time_tuple[:3]
a_date = datetime.date(year, month, day)
# print(a_date, type(a_date))
# print(a_date)
nextMonth = a_date + relativedelta(months = monthValue)
# print(nextMonth)
# print(nextMonth.strftime("%Y%m%d"))
return nextMonth.strftime("%Y%m%d")
#获取前后n天的日期
def getBeforeAfterDayDate(dateStr, dayValue):
fmt = '%Y%m%d'
time_tuple = time.strptime(dateStr, fmt)
year, month, day = time_tuple[:3]
a_date = datetime.date(year, month, day)
# print(a_date, type(a_date))
# print(a_date)
nextMonth = a_date + relativedelta(days = dayValue)
# print(nextMonth)
# print(nextMonth.strftime("%Y%m%d"))
return nextMonth.strftime("%Y%m%d")
#获取每个月的天数,dateStr为YYYYMMDD
def getDayCountForEachMonth(dateStr):
fmt = '%Y%m%d'
time_tuple = time.strptime(dateStr, fmt)
year, month, day = time_tuple[:3]
a_date = datetime.date(year, month, day)
#print(year)
#print(month)
monthRange = calendar.monthrange(year, month)
# dayCount = calendar.monthlen(year, month)
return monthRange[1]