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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18200530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:12:03  来源:igfitidea点击:

get the last sunday and saturday's date in python

pythondatedatetime

提问by fox

Looking to leverage datetimeto get the date of beginning and end of the previous week, sunday to saturday.

希望利用杠杆datetime来获取上一周(周日到周六)的开始和结束日期。

So, if it's 8/12/13 today, I want to define a function that prints:

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

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

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

How do I go about writing this?

我该怎么写这个?

EDIT: okay, so there seems to be some question about edge cases. For saturdays, I want the same week, for anything else, I'd like the calendar week immediately preceding today's date.

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

回答by falsetru

datetime.date.weekdayreturns 0for Monday. You need to adjust that.

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

Try following:

尝试以下操作:

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

If you are allowed to use python-dateutil:

如果您被允许使用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'

回答by Rob?

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

回答by jason

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)

Note that this seems to survive all of your cases:

请注意,这似乎适用于您的所有情况:

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

回答by kartheek

I found the best answer from hereworking fine in my case

我从这里找到了在我的情况下工作正常的最佳答案

try this

尝试这个

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)

回答by jozo

When I was dealing with this I came with this solution:

当我处理这个问题时,我提出了这个解决方案:

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)

So OP could use it as:

因此 OP 可以将其用作:

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

回答by sameer.joshi

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)