主要实现功能为提供一个时间节点,获取时间节点星期内的所有日期。

执行结果为:

开始日期: 2020-12-28
结束日期: 2021-01-03
2020-12-28
2020-12-29
2020-12-30
2020-12-31
2021-01-01
2021-01-02
2021-01-03

以下为Python脚本内容:

# 获取指定日期内一周启始天数 By:admin@ym68.cc
from datetime import date
# year=input("请输入年月日(以-为分隔符):")
year = 2020
month = 7
day = 30
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
    if month == 8 or month == 1:
        # 上个月日期
        Old_Day_Max = 31
    elif month == 3:
        if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) and (month == 2):
            Old_Day_Max = 29
        elif month == 2:
            Old_Day_Max = 28
    else:
        Old_Day_Max = 30
    # 当月日期
    Old_Day_New_Max = 31
else:
    Old_Day_Max = 31
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0) and (month == 2):
        Old_Day_New_Max = 29
    elif month == 2:
        Old_Day_New_Max = 28
    else:
        Old_Day_New_Max = 30
a = date(year, month, day)
old_year_month_day = a.weekday()
if day - old_year_month_day <= 0:
    if month == 1:
        Old_Month = 12
        Old_Year = year - 1
    else:
        Old_Month = month - 1
else:
    Old_Month = month
if old_year_month_day - day < 0:
    New_Month = month + 1
else:
    New_Month = month
# 判断月前及月后可能的日期
if day < 8:
    if day - old_year_month_day <= 0:
        Start_Day = Old_Day_Max - (old_year_month_day - day)
        if month == 1:
            Start_Date_Time = date(Old_Year, 12, Start_Day)

        else:
            Start_Date_Time = date(year, Old_Month, Start_Day)
        Stop_Day = (day - old_year_month_day) + 6
        Stop_Date_Time = date(year, month, Stop_Day)
    elif day - old_year_month_day > 0:
        Start_Day = day - old_year_month_day
        Start_Date_Time = date(year, Old_Month, Start_Day)
        Stop_Day = (day - old_year_month_day) + 6
        Stop_Date_Time = date(year, month, Stop_Day)

elif day > 22:
    Start_Day = day - old_year_month_day
    Start_Date_Time = date(year, Old_Month, Start_Day)
    if ((day - old_year_month_day) + 6) - Old_Day_New_Max > 0:
        month += 1
        Stop_Day = ((day - old_year_month_day) + 6) - Old_Day_New_Max
        Stop_Date_Time = date(year, month, Stop_Day)
    else:
        Stop_Day = (day - old_year_month_day) + 6
        Stop_Date_Time = date(year, month, Stop_Day)
elif day - old_year_month_day > 0:
    Start_Day = day - old_year_month_day
    Start_Date_Time = date(year, Old_Month, Start_Day)
    Stop_Day = (day - old_year_month_day) + 6
    Stop_Date_Time = date(year, month, Stop_Day)
print("开始日期:", Start_Date_Time)
print("结束日期:", Stop_Date_Time)
New_Start_Day = Start_Day
New_Day = 1
test = 0
while test != 1:
    if day > 24:
        if New_Start_Day <= Old_Day_New_Max:
            print(date(year, Old_Month, New_Start_Day))
        else:
            print(date(year, New_Month, New_Day))
            New_Day += 1
    else:
        if New_Start_Day <= Old_Day_Max:
            if month == 1 and day - old_year_month_day <= 0:
                print(date(Old_Year, Old_Month, New_Start_Day))
            elif day - old_year_month_day > 0:
                print(date(year, Old_Month, New_Start_Day))
            elif day - old_year_month_day < 0:
                print(date(year, Old_Month, New_Start_Day))
        else:
            print(date(year, New_Month, New_Day))
            New_Day += 1
    if New_Start_Day == Start_Day + 6:
        test = 1
    else:
        if New_Start_Day >= Start_Day + 1:
            New_Start_Day += 1
        else:
            New_Start_Day = Start_Day + 1

该文章采用「CC 协议」,转载必须注明作者和本文链接.
分类: Python