python判断某一天是周几

博主已经封装好了,使用get_week()就可以了

import datetime


def get_week(year: int, month: int, day: int) -> str:
    """
    :param year: 年
    :param month: 月
    :param day: 日
    :return: 返回1-6代表周一到周六,返回7代表周日
    """
    check_week = datetime.datetime(year, month, day).strftime("%w")
    if check_week == '0':  # 由于周日会被认为是0,这里返回7
        return '7'
    else:
        return check_week


if __name__ == "__main__":
    check_time: str = get_week(2021, 3, 7)
    print('星期', check_time)  # 星期 7

猜你喜欢

转载自blog.csdn.net/weixin_35757704/article/details/114435867