在python中获取最后一个星期日和星期六的约会

时间:2013-08-13 04:11:01

标签: python date datetime

希望利用datetime获取上周开始和结束的日期,星期日到星期六。

所以,如果它今天是8/12/13,我想定义一个打印的函数:

Last Sunday was 8/4/2013 and last Saturday was 8/10/2013

我该怎么写这个?

编辑:好的,所以似乎有一些关于边缘情况的问题。对于星期六,我想要同一周,除了其他任何事情,我想要在today日期之前的日历周。

7 个答案:

答案 0 :(得分:20)

datetime.date.weekday周一返回0。你需要调整它。

请尝试以下操作:

>>> import datetime
>>> today = datetime.date.today()
>>> today
datetime.date(2013, 8, 13)
>>> idx = (today.weekday() + 1) % 7 # MON = 0, SUN = 6 -> SUN = 0 .. SAT = 6
>>> idx
2
>>> sun = today - datetime.timedelta(7+idx)
>>> sat = today - datetime.timedelta(7+idx-6)
>>> 'Last Sunday was {:%m/%d/%Y} and last Saturday was {:%m/%d/%Y}'.format(sun, sat)
'Last Sunday was 08/04/2013 and last Saturday was 08/10/2013'

如果您被允许使用python-dateutil

>>> import datetime
>>> from dateutil import relativedelta
>>> today = datetime.datetime.now()
>>> start = today - datetime.timedelta((today.weekday() + 1) % 7)
>>> sat = start + relativedelta.relativedelta(weekday=relativedelta.SA(-1))
>>> sun = sat + relativedelta.relativedelta(weekday=relativedelta.SU(-1))
>>> 'Last Sunday was {:%m/%d/%Y} and last Saturday was {:%m/%d/%Y}'.format(sun, sat)
'Last Sunday was 08/04/2013 and last Saturday was 08/10/2013'

答案 1 :(得分:8)

我发现here的最佳答案在我的案例中正常工作

试试这个

from datetime import datetime,timedelta
import time

def last_day(d, day_name):
    days_of_week = ['sunday','monday','tuesday','wednesday',
                        'thursday','friday','saturday']
    target_day = days_of_week.index(day_name.lower())
    delta_day = target_day - d.isoweekday()
    if delta_day >= 0: delta_day -= 7 # go back 7 days
    return d + timedelta(days=delta_day)

答案 2 :(得分:6)

from datetime import date

def satandsun(input):
    d = input.toordinal()
    last = d - 6
    sunday = last - (last % 7)
    saturday = sunday + 6
    print date.fromordinal(sunday)
    print date.fromordinal(saturday)

请注意,这似乎可以解决您的所有情况:

>>> satandsun(date(2013, 8, 10))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 11))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 12))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 13))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 14))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 15))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 16))
2013-08-04
2013-08-10
>>> satandsun(date(2013, 8, 17))
2013-08-11
2013-08-17

答案 3 :(得分:5)

>>> today = date.today().toordinal()
>>> lastWeek = today-7
>>> sunday = lastWeek - (lastWeek % 7)
>>> saturday = sunday + 6
>>> print "Last Sunday was %s and last Saturday was %s" % (date.fromordinal(sunday), date.fromordinal(saturday))
Last Sunday was 2013-08-04 and last Saturday was 2013-08-10

答案 4 :(得分:0)

当我处理这个问题时,我想到了以下解决方案:

from datetime import datetime, timedelta

def prior_week_end():
    return datetime.now() - timedelta(days=((datetime.now().isoweekday() + 1) % 7))

def prior_week_start():
    return prior_week_end() - timedelta(days=6)

因此OP可以将其用作:

'Last Sunday was {:%m/%d/%Y} and last Saturday was {:%m/%d/%Y}'.format(prior_week_start(), prior_week_end())

答案 5 :(得分:0)

import datetime

d = datetime.datetime.today()    
sat_offset = (d.weekday() - 5) % 7  
saturday = d - datetime.timedelta(days=sat_offset)    
print("Last Saturday was on", saturday)
sun_offset = (d.weekday() - 6) % 7
sunday = d - datetime.timedelta(days=sun_offset)
print("Last Sunday was on", sunday)

答案 6 :(得分:0)

以下代码对我有用:

    today = datetime.date.today()
    last_sunday_offset = today.weekday() + 1  # convert day format mon-sun=0-6 => sun-sat=0-6 
    last_sunday = today - datetime.timedelta(days=last_sunday_offset)

注意:上面我采用了正常的工作日(星期一为 0),但在 isoweekday 星期一为 1。 有关更多详细信息,您可以查看 python 内置包 datetime.pyweekday() 和 isoweekday() 方法:

    def weekday(self):
        "Return day of the week, where Monday == 0 ... Sunday == 6."
        return (self.toordinal() + 6) % 7

    # Day-of-the-week and week-of-the-year, according to ISO

    def isoweekday(self):
        "Return day of the week, where Monday == 1 ... Sunday == 7."
        # 1-Jan-0001 is a Monday
        return self.toordinal() % 7 or 7```