数据统计、测试计划等经常以周为单位来统计数据。所以该方法也经常用到
import datetime
import time
class Date:
def day(self, add_day=0, day=None):
"""
返回年月日
:param add_day: 日可加可减
:param day:自定义日
:return:
"""
assert type(add_day) == int, 'add_day参数必须是int类型'
if day == None:
year_month_day = str(datetime.date.today() + datetime.timedelta(days=add_day))
else:
year_month_day = str(datetime.datetime.strptime(day, '%Y-%m-%d').date() + datetime.timedelta(days=add_day))
return year_month_day
def weekday(self, add=0):
"""
返回当天星期几
:return:
"""
from datetime import datetime
day = datetime.strptime(self.day(add_day=add), "%Y-%m-%d").date()
import datetime
number = datetime.date.weekday(day)
return number + 1
def week_date(self,number=0):
"""
获取某周星期一到星期天的日期
:return:
"""
num = self.weekday()-1
number = number*7
req = []
if num == 0:
for i in range(7):
req.append(self.day(i+number))
elif num == 1:
for i in range(7):
req.append(self.day(i+number - 1))
elif num == 2:
for i in range(7):
req.append(self.day(i+number - 2))
elif num == 3:
for i in range(7):
req.append(self.day(i+number - 3))
elif num == 4:
for i in range(7):
req.append(self.day(i+number - 4))
elif num == 5:
for i in range(7):
req.append(self.day(i+number - 5))
elif num == 6:
for i in range(7):
req.append(self.day(i+number - 6))
return req