如何获得上周五?

时间:2012-10-02 08:37:49

标签: python datetime

以下代码应于上周五16:00:00返回。但它返回Friday of previous week。如何解决?

now = datetime.datetime.now()
test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1))
test = test.replace(hour=16,minute=0,second=0,microsecond=0)

UPD。我现在使用以下方法 - 它是最好的吗?

now = datetime.datetime.now()
if datetime.datetime.now().weekday() > 4:
    test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4))
else:
    test = (now - datetime.timedelta(days=now.weekday()) + timedelta(days=4, weeks=-1))
test = test.replace(hour=16,minute=0,second=0,microsecond=0)

UPD2。举个例子。我们假设今天 2012年10月5日。如果当前时间等于或小于 16:00 ,则应返回 2012年9月28日,否则 - 2012年10月5日。< / p>

6 个答案:

答案 0 :(得分:16)

dateutil library非常适合这样的事情:

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta, FR
>>> datetime.now() + relativedelta(weekday=FR(-1))
datetime.datetime(2012, 9, 28, 9, 42, 48, 156867)

答案 1 :(得分:6)

与关联问题一样,您需要使用datetime.date个对象而不是datetime.datetime。要最终获得datetime.datetime,您可以使用datetime.datetime.combine()

import datetime

current_time = datetime.datetime.now()

# get friday, one week ago, at 16 o'clock
last_friday = (current_time.date()
    - datetime.timedelta(days=current_time.weekday())
    + datetime.timedelta(days=4, weeks=-1))
last_friday_at_16 = datetime.datetime.combine(last_friday, datetime.time(16))

# if today is also friday, and after 16 o'clock, change to the current date
one_week = datetime.timedelta(weeks=1)
if current_time - last_friday_at_16 >= one_week:
    last_friday_at_16 += one_week

答案 2 :(得分:2)

这是从Jon Clements借来的,但却是完整的解决方案:

>>> from datetime import datetime
>>> from dateutil.relativedelta import relativedelta, FR
>>> lastFriday = datetime.now() + relativedelta(weekday=FR(-1))
>>> lastFriday.replace(hour=16,minute=0,second=0,microsecond=0)
datetime.datetime(2012, 9, 28, 16, 0, 0, 0)

答案 3 :(得分:1)

原则与your other question中的原则相同。

获取当周的周五,如果我们稍后,则减去一周。

import datetime
from datetime import timedelta
now = datetime.datetime.now()
today = now.replace(hour=16,minute=0,second=0,microsecond=0)
sow = (today - datetime.timedelta(days=now.weekday()))
this_friday = sow + timedelta(days=4)
if now > this_friday:
     test = this_friday
else:
     test = this_friday + timedelta(weeks=-1)

答案 4 :(得分:0)

可能是蹩脚但对我来说最简单。获取当前月份的最后一天并开始检查一个循环(在上周五找到最大循环之前不会花费任何费用)周五。如果最后一天不是星期五减量和前一天的检查。

import calendar
from datetime import datetime, date

def main():
    year = datetime.today().year    
    month = datetime.today().month  
    x = calendar.monthrange(year,month)
    lastday = x[1]
    while True:
        z = calendar.weekday(year, month, lastday)
        if z != 4:
            lastday -= 1
        else:
            print(date(year,month,lastday))
            break

if __name__ == "__main__":
    main()

答案 5 :(得分:0)

没有依赖的最简单的解决方案:

from datetime import datetime, timedelta

def get_last_friday():
    now = datetime.now()
    closest_friday = now + timedelta(days=(4 - now.weekday()))
    return (closest_friday if closest_friday < now
            else closest_friday - timedelta(days=7))