将 6 位数字转换为工作日 (python)

Convert 6 digit number into weekday (python)

我目前正在尝试将 6 位数字转换为星期几。例如,我想将“150102”转换为工作日星期五“15”是2015年,“01”是月份,“02”是日期

请注意,我想在不导入任何时间函数和使用语言 python 的情况下执行此操作。

好吧,根据您的评论判断,这是一项作业(这意味着它应该被标记为作业)。所以我担心我的解决方案会含糊不清,但请提供一些关于 a(阅读,可能不是最好的)解决方案的指导。

首先,您想要将给定的字符串拆分为 year/month/date。

public class Date {
    private int year, month, date;

    public Date(final String given) {
        // TODO - Extract year / month / date.
        // Validate year is supported, month exists and date
        // exists in month...
    }
}

接下来,您(可能)需要一些您知道工作日(例如,2000 年 1 月 1 日;星期六)的固定点。

然后使用您对 year/leap-year 中的天数、每个月等的了解,找出自 提供的参数日期以来 有多少天。

static int daysSinceKnown(Date date) {
    // TODO - How many years since, how many of those were leap-years, etc.
}

最后,模运算可以从该函数的 return 中得出工作日。 (具体来说,查看那个值 mod 7 (%7) 会给你一个数字 0-6 [负数不会很好地工作,但你可以在验证年份时抓住它]可以解释为星期几。)