-2

I am looking into using datetime to find the date one week before now()... I was wondering if there was a way to use algebraic calculations directly with datetime format?

This is the code I am using.

import datetime

def get_week():
    year = datetime.date.today().year
    month = datetime.date.today().month
    day = datetime.date.today().day
    month1 = month
    year1 = year
    day1 = day-7

    if day < 7:
        day1 = 26 # In my case, this date back is plenty enough
        if month == 1:
                month1 = 12
                year1 = year-1

    return year1, month1, day1, year, month, day
petezurich
  • 9,280
  • 9
  • 43
  • 57
poteus
  • 11
  • 1

1 Answers1

0

Just use what the datetime module offers:

from datetime import datetime, timedelta
datetime.now() - timedelta(days=7)

# datetime.datetime(2018, 11, 4, 16, 58, 37, 328226)
Thierry Lathuille
  • 23,663
  • 10
  • 44
  • 50