Pergunta

I need to make a loop that looks back across a given calculation for the last 7 days. I've currently structured my loop as follows, however I'm having an issue when hitting a prior month's days.

cmm = datetime.datetime.now().strftime("%m")#current month
cdd = datetime.datetime.now().strftime("%d") #current date
cyyyy = datetime.datetime.now().strftime("20%y") #current year
refdate = cyyyy + '-' + cmm + '-' + cdd + 'T'

D7 = [1,2,3,4,5,6,7]
for i in D7:

date = cyyyy + '-' + cmm + '-' + str(int(cdd) - i) + 'T'
print(date, "  ", "NUU", " NUU1", " NUU2", " NUU3", " NUU4", " NUU5", " NUU6")

Output:

2013-10-3T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10-2T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10-1T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10-0T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10--1T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10--2T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6
2013-10--3T    NUU  NUU1  NUU2  NUU3  NUU4  NUU5  NUU6

The last 4 dates listed here are clearly not adequate.

The dates are being used in the calculation of the values of NUU[0-6] using regex in some PyMongo queries.

Not sure which direction to run here without wasting a whole lot of time. tuple_time?

Foi útil?

Solução

Why not start from something like...

from datetime import datetime, timedelta

# initial_date = datetime.today()
initial_date = datetime(2000, 3, 2)
interval = timedelta(days = 6)
increment = timedelta(days = 1)

i = initial_date - interval
while i <= initial_date:
    print(i)
    i += increment

...and format each date as needed?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top