web-dev-qa-db-fra.com

obtenez le dernier dimanche et samedi de la date en python

Vous cherchez à tirer parti de datetime pour obtenir la date du début et de la fin de la semaine précédente, du dimanche au samedi.

Donc, si nous sommes le 12/08/13 aujourd'hui, je veux définir une fonction qui affiche:

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

Comment puis-je écrire cela?

EDIT: OK, donc il semble y avoir une question sur les cas Edge. Pour les samedis, je veux la même semaine, pour toute autre chose, j'aimerais la semaine du calendrier précédant immédiatement la date de today.

15
fox

datetime.date.weekday retourne 0 pour lundi. Vous devez ajuster cela.

Essayez de suivre:

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

Si vous êtes autorisé à utiliser 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'
17
falsetru

J'ai trouvé la meilleure réponse de ici fonctionne bien dans mon cas

essaye ça

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

Notez que cela semble survivre à tous vos cas:

>>> 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
6
jason
>>> 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
5
Robᵩ

Quand j'ai eu affaire à cela, je suis venu avec cette 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)

Donc, OP pourrait l'utiliser comme:

'Last Sunday was {:%m/%d/%Y} and last Saturday was {:%m/%d/%Y}'.format(prior_week_start(), prior_week_end())
0
jozo
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)
0
sameer.joshi