python对周内日期的获取代码如下:
import datetime
def get_current_week():
monday,tuesday,wednesday,thursday,friday,saturday,sunday = datetime.date.today(),datetime.date.today(),datetime.date.today(),datetime.date.today(),datetime.date.today(),datetime.date.today(),datetime.date.today()
one_day = datetime.timedelta(days=1)
while monday.weekday() != 0:
# 周一的日期
monday -= one_day
print(monday)
while tuesday.weekday() != 1:
# 周二的日期
if tuesday.weekday() < 1:
tuesday += one_day
elif tuesday.weekday() > 1:
tuesday -= one_day
else:
pass
print(tuesday)
while wednesday.weekday() != 2:
# 周三的日期
if wednesday.weekday() < 2:
wednesday += one_day
elif wednesday.weekday() > 2:
wednesday -= one_day
else:
pass
print(wednesday)
while thursday.weekday() != 3:
# 周四的日期
if thursday.weekday() < 3:
thursday += one_day
elif thursday.weekday() > 3:
thursday -= one_day
else:
pass
print(thursday)
while friday.weekday() != 4:
# 周五的日期
if friday.weekday() < 4:
friday += one_day
elif friday.weekday() > 4:
friday -= one_day
else:
pass
print(friday)
while saturday.weekday() != 5:
# 周六的日期
if saturday.weekday() < 5:
saturday += one_day
elif saturday.weekday() > 5:
saturday -= one_day
else:
pass
print(saturday)
while sunday.weekday() != 6:
# 周天的日期
sunday += one_day
print(sunday)
get_current_week()
|