Datetime skips certain dates

I use this method to get the date. Now I want to skip all weekends during this period and only return to the working day. What should I do?
s = datetime.date (2017 1)
e = datetime.date (2018 1)

t = datetime.timedelta (days=1)
while s < = e:

s += t

Mar.30,2021

consider the python.org/3/library/datetime.html-sharpdatetime.date.weekday" rel=" nofollow noreferrer "> weekday () method, which returns Arabic numerals for the day of the week. Note that Monday is 0 and Sunday is 6 .

reference for specific implementation:

import datetime
s = datetime.date(2017,1,1)
e = datetime.date(2018,1,1)


def get_weekdays(s,e): 
    """
    :param s 
    :param e 
    :return: datetime.datelist
    """
    result = list()
    -sharp delta1
    delta = datetime.timedelta(days=1)
    while s != e:
        s += delta
        -sharp 
        if s.weekday() in [0,1,2,3,4]:
            result.append(s)
    return result

weekdays = get_weekdays(s=s,e=e)

continue the answer from dei upstairs, and give you a code example. If you want to get the time range, for loop to assemble the string, then switch to datetime type, get the week by referring to the example below, and add an if to judge that you can skip Saturday and Sunday

.

pandas

import pandas as pd
dates = pd.date_range(start='20170101', end='20180101', freq='B')
Menu