大家有没有因为计算每一天的时间而困扰,今天帮助大家解决这个困扰,废话不多说直接上代码
import datetime
fmt = "%Y-%m-%d"
def get_monday(today=None, number=0) -> str:
"""
获取Monday的方法, 通过指定number来获得当前之后或者当前后的星期一的日期
number 为 0 是获取本周的星期一
number 为 1 是获取上周的星期一
"""
time_fmt = "%Y-%m-%d"
today = datetime.datetime.strptime(str(today), time_fmt) if today else datetime.datetime.today()
return datetime.datetime.strftime(today - datetime.timedelta(today.weekday()) - datetime.timedelta(days=number*7), time_fmt)
def get_date(date):
"""返回两个日期
本周一和下周一
"""
date_obj = datetime.datetime.strptime(date, "%Y-%m-%d")
next_week_monday = datetime.datetime.strftime(date_obj + datetime.timedelta(days=7), "%Y-%m-%d")
return date, next_week_monday