Home > Enterprise >  How can I replace datetime with While loop in Python
How can I replace datetime with While loop in Python

Time:11-19

from datetime import datetime
from datetime import timedelta

dateIs = datetime.today()
thisDay = datetime.strftime(dateIs, "%d")
thisDayInt = int(thisDay)
dateListTest = []
if(thisDayInt > 0):
    while thisDayInt > 1:
        thisDayInt -= 1
        dateIs.replace(day=thisDayInt,hour=0,minute=0,second=0)
        dateListTest.append(dateIs)
    
    print(dateListTest)

Output

[datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371), 
datetime.datetime(2021, 11, 18, 23, 38, 23, 687371)] 

What I want to do in this part is to get a list of the days from today to the beginning of the month one by one, but when I put them in the While loop, I cannot print the days by decrement. I don't know about a shorter path or where I'm skipping.

CodePudding user response:

List comprehension

You could use a list comprehension for this.

from datetime import datetime

dateIs = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
thisDay = dateIs.day

dateListTest = [dateIs.replace(day=d) for d  in range(thisDay-1, 0, -1)]

print(dateListTest)
[datetime.datetime(2021, 11, 17, 0, 0), 
 datetime.datetime(2021, 11, 16, 0, 0), 
 datetime.datetime(2021, 11, 15, 0, 0), 
 datetime.datetime(2021, 11, 14, 0, 0), 
 datetime.datetime(2021, 11, 13, 0, 0), 
 datetime.datetime(2021, 11, 12, 0, 0), 
 datetime.datetime(2021, 11, 11, 0, 0), 
 datetime.datetime(2021, 11, 10, 0, 0), 
 datetime.datetime(2021, 11, 9, 0, 0), 
 datetime.datetime(2021, 11, 8, 0, 0), 
 datetime.datetime(2021, 11, 7, 0, 0), 
 datetime.datetime(2021, 11, 6, 0, 0), 
 datetime.datetime(2021, 11, 5, 0, 0), 
 datetime.datetime(2021, 11, 4, 0, 0), 
 datetime.datetime(2021, 11, 3, 0, 0), 
 datetime.datetime(2021, 11, 2, 0, 0), 
 datetime.datetime(2021, 11, 1, 0, 0)]

Pandas

You could also do it with date_range from Pandas, though you will end up with a list of datetime.date rather than datetime.datetime.

from datetime import datetime
import pandas as pd

dateIs = datetime.today().replace(hour=0, minute=0, second=0, microsecond=0)
thisDay = dateIs.day

dateListTestPandas =pd.date_range(start=dateIs.replace(day=thisDay-1), end=dateIs.replace(day=1), freq="-1D").date.tolist()

print(dateListTestPandas)
[datetime.date(2021, 11, 17), 
 datetime.date(2021, 11, 16), 
 datetime.date(2021, 11, 15), 
 datetime.date(2021, 11, 14), 
 datetime.date(2021, 11, 13), 
 datetime.date(2021, 11, 12), 
 datetime.date(2021, 11, 11), 
 datetime.date(2021, 11, 10), 
 datetime.date(2021, 11, 9), 
 datetime.date(2021, 11, 8), 
 datetime.date(2021, 11, 7), 
 datetime.date(2021, 11, 6), 
 datetime.date(2021, 11, 5), 
 datetime.date(2021, 11, 4), 
 datetime.date(2021, 11, 3), 
 datetime.date(2021, 11, 2), 
 datetime.date(2021, 11, 1)]
  • Related