it-swarm.com.de

holen Sie sich das Datum des letzten Sonntags und Samstags in Python

Nutzen Sie datetime, um das Datum für Anfang und Ende der Vorwoche von Sonntag bis Samstag zu ermitteln.

Wenn es heute der 12.08.13 ist, möchte ich eine Funktion definieren, die Folgendes ausgibt:

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

Wie schreibe ich das?

EDIT: Okay, es scheint also eine Frage zu Edge-Fällen zu geben. Für Samstage möchte ich dieselbe Woche, für alles andere möchte ich die Kalenderwoche unmittelbar vor dem Datum von today.

15
fox

datetime.date.weekday gibt 0 für Montag zurück. Sie müssen das anpassen.

Versuchen Sie Folgendes:

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

Wenn Sie python-dateutil verwenden dürfen:

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

Ich fand die beste Antwort von hier in meinem Fall funktioniert

versuche dies

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)

Beachten Sie, dass dies all Ihre Fälle zu überleben scheint:

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

Als ich mich damit befasste, kam ich mit dieser Lösung:

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 könnte OP es verwenden als:

'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