python获取当前日期所在的是本月第几周

def get_week(date_str=None):
    if date_str and isinstance(date_str, str):
        now_time = datetime.strptime(date_str + " 00:00:00", "%Y-%m-%d %H:%M:%S")
    else:
        now_time = datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)
    # 当月第一天
    one_time = now_time.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
    # 当前日期所在周的周一
    week_start_time = now_time - timedelta(days=now_time.weekday(), hours=now_time.hour, minutes=now_time.minute,
                                           seconds=now_time.second)
    # 当前日期所在周的周日
    week_end_time = week_start_time + timedelta(days=6, hours=23, minutes=59, seconds=59)

    # 当前日期处于本月第几周
    week_num = int(now_time.strftime('%W')) - int(one_time.strftime('%W')) + 1

    # 当前所处月份
    month_num = int(now_time.strftime('%m'))

    # 当前年份
    year_num = int(now_time.strftime('%Y'))

 

你可能感兴趣的:(python,几周,星期,python)