Python 从工作日获取日期

Python get date from weekday

假设我有 11 个课程要完成。我没有为这些 session 设置日期,而是只设置了一个 session 发生的工作日。假设在安排这些 session 时,我选择了周一、周二和周三。这意味着在今天之后,我希望日期到我的 session 的 11 天,即从现在开始的 4 个星期一、4 个星期二和 3 个星期三,之后我的 session 将完成。

我想自动获取这几天的日期,直到总共有11个日期。

我真的希望这是有道理的...请帮助我。我已经连续 3 个小时挠头了。

谢谢,

您可以使用 pd.date_range and the CustomBusinessDay 对象轻松完成此操作。

您可以使用 CustomBusinessDay 来指定您的 "business days" 并从中创建您的日期范围:

import pandas
from datetime import date

session_days = pd.offset.CustomBusinessDay(weekmask="Mon Tue Wed")
dates = pd.date_range(date.today(), freq=session_days, periods=11)

我不久前就弄明白了,但我的互联网死了。只需要 Dunhill 和一些休息。

import datetime


def get_dates():
    #This is the max number of dates you want. In my case, sessions.
    required_sessions = 11
    #These are the weekdays you want these sessions to be
    days = [1,2,3]
    #An empty list to store the dates you get
    dates = []
    #Initialize a variable for the while loop
    current_sessions = 0
    #I will start counting from today but you can choose any date
    now = datetime.datetime.now()
    #For my use case, I don't want a session on the same day I run this function.
    #I will start counting from the next day
    if now.weekday() in days:
        now = now + datetime.timedelta(days=1)

    while current_sessions != required_sessions:
        #Iterate over every day in your desired days
        for day in days:
            #Just a precautionary measure so the for loops breaks as soon as you have the max number of dates
            #Or the while loop will run for ever
            if current_sessions == required_sessions:
                break
            #If it's Saturday, you wanna hop onto the next week
            if now.weekday() == 6:
                #Check if Sunday is in the days, add it
                if 0 in days:
                    date = now + datetime.timedelta(days=1)
                    dates.append(date)
                    current_sessions += 1
                    now = date
            else:
                #Explains itself.
                if now.weekday() == day:
                    dates.append(now)
                    now = now + datetime.timedelta(days=1)
                    current_sessions += 1
                #If the weekday today is greater than the day you're iterating over, this means you've iterated over all the days in a NUMERIC ORDER
                #NOTE: This only works if the days in your "days" list are in a correct numeric order meaning 0 - 6. If it's random, you'll have trouble
                elif not now.weekday() > day:
                    difference = day - now.weekday()
                    date = now + datetime.timedelta(days=difference)
                    dates.append(date)
                    now = date
                    current_sessions += 1
        #Reset the cycle after the for loop is done so you can hop on to the next week.
        reset_cycle_days = 6 - now.weekday()
        if reset_cycle_days == 0:
            original_now = now + datetime.timedelta(days=1)
            now = original_now
        else:
            original_now = now + datetime.timedelta(days=reset_cycle_days)
            now = original_now
    for date in dates:(
        print(date.strftime("%d/%m/%y"), date.weekday()))

顺便说一句,我知道这个答案与@Daniel Geffen 的答案相比毫无意义。如果我是你,我肯定会选择他的答案,因为它很简单。这只是我对我自己的问题的贡献,以防有人想跳入 "technicalities" 了解如何仅使用 datetime 完成它。对我来说,这最有效,因为我在 Python3.7 .

中遇到 _bz2 问题

谢谢大家的帮助。