简体   繁体   English

Python 从工作日获取日期

[英]Python get date from weekday

Let's say I have 11 Sessions for myself to complete.假设我要完成 11 个会话。 I haven't set dates for these sessions but rather just weekdays where one session would take place.我没有为这些会议设定日期,而只是在工作日举行一场 session。 Let's say when scheduling these sessions, I chose MON, TUE and WED.假设在安排这些会议时,我选择了 MON、TUE 和 WED。 This means that after today, I want the dates to 11 my sessions which would be 4 Mondays, 4 Tuesdays and 3 Wednesdays from now after which my sessions will be completed.这意味着今天之后,我希望将日期设置为 11 个我的会话,即从现在开始的 4 个星期一、4 个星期二和 3 个星期三,之后我的会话将完成。

I want to automatically get the dates for these days until there are 11 dates in total.我想自动获取这些天的日期,直到总共有 11 个日期。

I really hope this makes sense... Please help me.我真的希望这是有道理的......请帮助我。 I've been scratching my head over this for 3 hours straight.我已经连续 3 个小时为这个问题挠头了。

Thanks,谢谢,

You can use pd.date_range and the CustomBusinessDay object to do this very easily.您可以使用pd.date_rangeCustomBusinessDay object 轻松完成此操作。

You can use the CustomBusinessDay to specify your "business days" and create your date range from it:您可以使用 CustomBusinessDay 指定您的“工作日”并从中创建您的日期范围:

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)

I figured it out a while ago but my internet died.我前一阵子想通了,但我的互联网死了。 All it took was Dunhill and some rest.只需要登喜路和一些 rest。

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()))

Btw, I know this answer is pointless compared to @Daniel Geffen 's answer.顺便说一句,我知道这个答案与@Daniel Geffen 的答案相比毫无意义。 If I were you, I would definitely choose his answer as it is very simple.如果我是你,我肯定会选择他的答案,因为它很简单。 This was just my contribution to my own question in case anyone would want to jump into the "technicalities" of how it's done by just using datetime.这只是我对我自己的问题的贡献,以防有人想通过仅使用 datetime 来了解它是如何完成的“技术”。 For me, this works best as I'm having issues with _bz2 in Python3.7.对我来说,这最有效,因为我在 Python3.7 中遇到了_bz2的问题。

Thank you all for your help.谢谢大家的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM