Date count of days when it has turn of year

Asked

Viewed 61 times

4

I’m having trouble generating an iteration where dates are displayed from a given day to the most current day. this problem happens when it has the "turn of the year" and I put a condition of "stop".

Code with wrong return:

df0 = pd.DataFrame({"datasolicitada":['21/12/2020 00:00','18/12/2020 00:00']})

data = df0['datasolicitada'].max()
data1 = datetime.strptime(data, "%d/%m/%Y %H:%M")
data2 = data1.strftime("%d/%m/%Y")
datahj = datetime.today().strftime("%d/%m/%Y")

for i in range(30):
    dia = (data1 + timedelta(days=i)).strftime("%d/%m/%Y")
    if datahj > dia:
        print (dia)

If I run only the print of the count, it is returned to me until the sum according to the range

for i in range(30):
    dia = (data1 + timedelta(days=i)).strftime("%d/%m/%Y")
    print (dia)

Results:

    if datahj > dia:
        print (dia)
01/01/2021
02/01/2021
03/01/2021
04/01/2021
05/01/2021

if I change the sign of the condition:

    if datahj < dia:
        print (dia)
21/12/2020
22/12/2020
23/12/2020
24/12/2020
25/12/2020
26/12/2020
27/12/2020
28/12/2020
29/12/2020
30/12/2020
31/12/2020
07/01/2021
08/01/2021
09/01/2021
10/01/2021
11/01/2021
12/01/2021
13/01/2021
14/01/2021
15/01/2021
16/01/2021
17/01/2021
18/01/2021
19/01/2021

result you would like to have:

21/12/2020
22/12/2020
23/12/2020
24/12/2020
25/12/2020
26/12/2020
27/12/2020
28/12/2020
29/12/2020
30/12/2020
31/12/2020
01/01/2021
02/01/2021
03/01/2021
04/01/2021
05/01/2021

1 answer

3


The problem is that strftime returns a string containing the date representation, not the date itself.

If you want to compare only the date, without taking into account the time, you can convert everything to date, instead of using datetime:

from datetime import datetime, timedelta, date

dt_inicio = datetime.strptime('21/12/2020 00:00', "%d/%m/%Y %H:%M").date()
hoje = date.today()
dt = dt_inicio
while dt < hoje:
    print(dt.strftime("%d/%m/%Y"))
    dt += timedelta(days=1)

Notice I only use strftime when printing the date. But when comparing to the current date I use the objects themselves date (used date.today() instead of datetime.today(), because from what I understand you don’t want to take into account the schedule).

And in the loop I will add 1 day until the date is equal to today.


But if the idea is to advance at most 30 days or until the current date (whichever occurs first), would be like this:

from datetime import datetime, timedelta, date
 
dt_inicio = datetime.strptime('21/12/2020 00:00', "%d/%m/%Y %H:%M").date()
hoje = date.today()
for i in range(30):
    dt = dt_inicio + timedelta(days=i)
    if dt < hoje:
        print(dt.strftime("%d/%m/%Y"))
    else: break # encerra o for

That is, I only print out the date if it is earlier than the current date, and otherwise I already close the loop.


And just to clear up the mess you made...

Dates have no format

As I said before here, here and here, dates have no format.

A date is just a concept, an idea: it represents a specific point in the calendar.

The date of "1 January 1970", for example, represents this: the specific point of the calendar that corresponds to the first day of January 1970. To express this idea in text form, I can write it in different ways:

  • 01/01/1970 (a common format in many countries, including Brazil)
  • 1/1/1970 (American format, reversing day and month)
  • 1970-01-01 (the format ISO 8601)
  • First of January 1970 (in good Portuguese)
  • January 1st, 1970 (in English)
  • 1970 年 1 月 1 日 (in Japanese)
  • and many others...

Note that each of the above formats is different, but all represent the same date (the same numerical values of the day, month and year).

In the case of Python, an object datetime or date represents a date (these objects have no format, only values representing the date).

When you use strftime is returning a string (a representation of the date in a given format). But a string is not a date: strings are compared lexicographically, then even digits (such as 0, 1, etc.) are treated as characters. Dates are compared by taking into account values (year, month, day, etc), so comparing dates is not the same as comparing strings (even if strings represent dates).

  • 1

    thank you very much for the explanation, I will read more about, hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.