查找给定日期后第一个星期一的日期

时间:2011-07-02 17:32:37

标签: python date

鉴于特定日期,比如2011-07-02,如何在该日期之后找到下周一(或该工作日的任何工作日)的日期?

11 个答案:

答案 0 :(得分:94)

import datetime
def next_weekday(d, weekday):
    days_ahead = weekday - d.weekday()
    if days_ahead <= 0: # Target day already happened this week
        days_ahead += 7
    return d + datetime.timedelta(days_ahead)

d = datetime.date(2011, 7, 2)
next_monday = next_weekday(d, 0) # 0 = Monday, 1=Tuesday, 2=Wednesday...
print(next_monday)

答案 1 :(得分:37)

这是上面稍微重要的答案的简洁和通用的替代方案。

# Returns the date of the next given weekday after
# the given date. For example, the date of next Monday.
# NB: if it IS the day we're looking for, this returns 0.
# consider then doing onDay(foo, day + 1).
onDay = lambda date, day: date + datetime.timedelta(days=(day-date.weekday()+7)%7)

答案 2 :(得分:21)

尝试

>>> dt = datetime(2011, 7, 2)
>>> dt + timedelta(days=(7 - dt.weekday()))
datetime.datetime(2011, 7, 4, 0, 0)

使用,下一个星期一是星期一后7天,星期二后6天,依此类推,并且还使用,Python的datetime类型报告星期一为0,... 。,星期日为6

答案 3 :(得分:4)

您可以开始添加一个日期对象,并在星期一时停止。

>>> d = datetime.date(2011, 7, 2)
>>> while d.weekday() != 0: #0 for monday
...     d += datetime.timedelta(days=1)
... 
>>> d
datetime.date(2011, 7, 4)

答案 4 :(得分:3)

这是响铃mod 7内的计算示例。

import datetime


def next_day(given_date, weekday):
    day_shift = (weekday - given_date.weekday()) % 7
    return given_date + datetime.timedelta(days=day_shift)

now = datetime.date(2018, 4, 15) # sunday
names = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday',    
         'saturday', 'sunday']
for weekday in range(7):
    print(names[weekday], next_day(now, weekday))

将打印:

monday 2018-04-16
tuesday 2018-04-17
wednesday 2018-04-18
thursday 2018-04-19
friday 2018-04-20
saturday 2018-04-21
sunday 2018-04-15

如你所见,下周一,星期二,星期三,星期四,星期六和星期六正确地给你。而且它也明白2018-04-15是一个星期天,并返回当前的星期日而不是下一个星期日。

我相信你会在7年后发现这个答案非常有用; - )

答案 5 :(得分:2)

另一个简单优雅的解决方案是使用pandas补偿 我发现在玩日期时非常有帮助和强大 - 如果你想要第一个星期日,只需将频率修改为freq ='W-SUN' - 如果您想要下几个星期日,请更改偏移量。天(天) - 使用pandas offsets可以忽略假期,仅适用于营业日等 您还可以使用apply方法在整个DataFrame上轻松应用此方法。

# Getting the closest monday from a given date
closest_monday = pd.date_range(start=date, end=date + offsets.Day(6), freq='W-MON')[0]

# Adding a 'ClosestMonday' column with the closest monday for each row in a pandas df using apply
# Require you to have a 'Date' column in your df
def get_closest_monday(row):
    return pd.date_range(start=row.Date, end=row.Date + offsets.Day(6), freq='W-MON')[0]

df['ClosestMonday'] = df.apply(lambda row: get_closest_monday(row), axis=1)

答案 6 :(得分:2)

import datetime

d = datetime.date(2011, 7, 2)
while d.weekday() != 0:
    d += datetime.timedelta(1)

答案 7 :(得分:0)

weekday = 0 ## Monday
dt = datetime.datetime.now().replace(hour=0, minute=0, second=0) ## or any specific date
days_remaining = (weekday - dt.weekday() - 1) % 7 + 1
next_dt = dt + datetime.timedelta(days_remaining)

答案 8 :(得分:0)

另一个替代方法是使用规则

from dateutil.rrule import rrule, WEEKLY, MO
from datetime import date

next_monday = rrule(freq=WEEKLY, dtstart=date.today(), byweekday=MO, count=1)[0]

规则文档:https://dateutil.readthedocs.io/en/stable/rrule.html

答案 9 :(得分:-1)

这将是给定日期后的下一个星期一的第一个:

import datetime

def get_next_monday(year, month, day):
    date0 = datetime.date(year, month, day)
    next_monday = date0 + datetime.timedelta(7 - date0.weekday() or 7)
    return next_monday

print get_next_monday(2011, 7, 2)
print get_next_monday(2015, 8, 31)
print get_next_monday(2015, 9, 1)

2011-07-04
2015年9月7日
2015年9月7日

答案 10 :(得分:-1)

通过列表理解?

OBJS =      http.o web_request.o
TARGET =    http

INC_PATH =  ./include
LIB_PATH=   ./libs

CC = gcc
os = windows

CFLAGS =    -c -g -I$(INC_PATH)
LDFLAGS =   -L$(LIB_PATH)

LIBS =   -lpthreadVC2 -lws2_32  -lcurl

$(TARGET): $(OBJS)
    $(CC)  -o $(TARGET)  $(OBJS) $(LDFLAGS) $(LIBS)

http.o: http.c
    $(CC) $(CFLAGS) http.c

web_request.o: web_request.c
    $(CC) $(CFLAGS) web_request.c

clean:
    rm $(TARGET) $(OBJS)