下个星期五怎么算?

时间:2012-01-10 09:37:02

标签: python datetime

如何计算下周五的日期?

7 个答案:

答案 0 :(得分:52)

@ taymon的回答有一些改进:

today = datetime.date.today()
friday = today + datetime.timedelta( (4-today.weekday()) % 7 )

4是星期五的工作日(0星期一,从星期一算起) ( (4-today.weekday()) % 7)是下周五的天数(%总是非负数)。

在看到@ ubuntu的回答后,我应该添加两件事:
我不确定周五= 4是否普遍正确。有些人周日开始他们的一周 星期五,这段代码在同一天返回。要获得下一个,请使用(3-today.weekday())%7+1。只是旧的x%n((x-1)%n)+1转化。

答案 1 :(得分:28)

首先,您需要datetime库:

import datetime

然后你需要一个开始日期;就是今天。

d = datetime.date.today()

从那里开始,你要继续前进直到星期五。 date.weekday方法表示周一到周日为0到6,所以:

while d.weekday() != 4:

如果当天不是星期五,您必须每天添加一天。要向date对象添加时间间隔,请使用timedelta对象。

    d += datetime.timedelta(1)

将所有内容放在一起,d最终将包含代表下周五的date个对象。请注意,如果今天是星期五,则此代码将在今天生成;如果你需要在下周五生产它,你可以调整它。

答案 2 :(得分:19)

以下是使用dateutil

执行此操作的方法
import datetime as DT
import dateutil.relativedelta as REL
today = DT.date.today()
print(today)
# 2012-01-10

rd = REL.relativedelta(days=1, weekday=REL.FR)
next_friday = today + rd
print(next_friday)
# 2012-01-13

(如果days = 1碰巧是星期五,today参数可确保“下周五”与today不同。)

答案 3 :(得分:0)

出于可读性考虑,我将使用strftime('%A')而不是weekday():

import datetime

d = datetime.date.today()

while d.strftime('%a') != 'Fri':
    d += datetime.timedelta(1)

答案 4 :(得分:0)

我发现此pendulum非常有用。只需一行

>>> pendulum.now().next(pendulum.FRIDAY).strftime('%Y-%m-%d')
'2019-04-26'

答案 5 :(得分:0)

import datetime 
def next_fri_13(start_date):
        next_friday = start_date + datetime.timedelta(((4 - today.weekday()) % 7))
        while True:
            if next_friday.day==13:
                thirteen_friday = next_friday
                break
            else:
                next_date = next_friday + datetime.timedelta(days=1)
                next_friday = next_date + datetime.timedelta(((4 -next_date.weekday())% 7))
        return thirteen_friday

    r = next_fri_13(datetime.date(2020, 2, 11)) print(r)

答案 6 :(得分:0)

您可以使用以下这些代码计算第二天的任何日期。

from datetime import datetime as dt
from datetime import timedelta

def get_weekday(day):
    days  = ["mon","tue","wed","thu","fri","sat","sun"]
    return days.index(day) + 1

def get_next_dayofweek_datetime(date_time, dayofweek):
    start_time_w = date_time.isoweekday()
    target_w = get_weekday(dayofweek)
    if start_time_w < target_w:
      day_diff = target_w - start_time_w
    else:
        day_diff = 7 - (start_time_w - target_w)

    return date_time + timedelta(days=day_diff)

def get_next_n_weekends_dates(date_time, weekday, n=2):
  days_list = []
  week_date_time = date_time
  while n > 0:
      week_date_time = get_next_dayofweek_datetime(week_date_time, weekday)
      days_list.append(week_date_time)
      n = n -1
  return  days_list

start_time = dt.strptime("2020-02-01 20:20:00", "%Y-%m-%d %H:%M:%S") # wednesday


print(get_next_dayofweek_datetime(start_time, "thu"))
print(get_next_dayofweek_datetime(start_time, "fri"))
print(get_next_dayofweek_datetime(start_time, "sat"))
print(get_next_dayofweek_datetime(start_time, "sun"))
print(get_next_dayofweek_datetime(start_time, "mon"))
print(get_next_dayofweek_datetime(start_time, "tue"))
print(get_next_dayofweek_datetime(start_time, "wed"))
print(get_next_dayofweek_datetime(start_time, "thu"))

print("get next two fridays or mote ")
print(get_next_n_weekends_dates(start_time, "fri", 2))

输出:

2020-02-06 10:20:00
2020-02-07 10:20:00
2020-02-08 10:20:00
2020-02-02 10:20:00
2020-02-03 10:20:00
2020-02-04 10:20:00
2020-02-05 10:20:00
2020-02-06 10:20:00
get next two fridays or mote 
[datetime.datetime(2020, 2, 7, 10, 20), datetime.datetime(2020, 2, 14, 10, 20)]