How to calculate next Friday?

前端 未结 7 1951
失恋的感觉
失恋的感觉 2020-12-13 13:34

How can I calculate the date of the next Friday?

相关标签:
7条回答
  • 2020-12-13 13:55

    Just for readability I would use strftime('%A') rather than weekday():

    import datetime
    
    d = datetime.date.today()
    
    while d.strftime('%a') != 'Fri':
        d += datetime.timedelta(1)
    
    0 讨论(0)
  • 2020-12-13 14:00

    You can calculate any next day dates using these this code below.

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

    output:

    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)]
    
    0 讨论(0)
  • 2020-12-13 14:02

    A certain improvement on @taymon`s answer:

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

    4 is Friday's weekday (0 based, counting from Monday).
    ( (4-today.weekday()) % 7) is the number of days till next friday (% is always non-negative).

    After seeing @ubuntu's answer, I should add two things:
    1. I'm not sure if Friday=4 is universally true. Some people start their week on Sunday.
    2. On Friday, this code returns the same day. To get the next, use (3-today.weekday())%7+1. Just the old x%n to ((x-1)%n)+1 conversion.

    0 讨论(0)
  • 2020-12-13 14:03

    To start off, you'll need the datetime library:

    import datetime
    

    Then you need a starting date; that is, today.

    d = datetime.date.today()
    

    Starting from there, you'll want to keep going forward until you reach Friday. The date.weekday method represents Monday through Sunday as 0 through 6, so:

    while d.weekday() != 4:
    

    If the current day isn't Friday, you'll have to add a day, one at a time. To add an interval of time to a date object, you use a timedelta object.

        d += datetime.timedelta(1)
    

    Put it all together, and d will ultimately contain a date object representing next Friday. Note that if today is Friday, this code will produce today; you can tweak it if you need it to produce next Friday instead.

    0 讨论(0)
  • 2020-12-13 14:08
    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)
    
    0 讨论(0)
  • 2020-12-13 14:17

    Here is how you could do it using 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
    

    (The days = 1 argument ensures that the "next Friday" is not the same as today in case today happens to be a Friday.)

    0 讨论(0)
提交回复
热议问题