Home > Software engineering >  Appending datetime to a list
Appending datetime to a list

Time:10-28

See the code below :

import datetime
data =[]
dt = datetime.datetime.today()
print(dt)
for i in range(60):
    delta = datetime.timedelta(minutes = i)
    dtnew = dt   delta
    data.append(dtnew)
 print(data)

This is what data looks like after running the code:

[datetime.datetime(2021, 10, 27, 23, 8, 18, 897419),....] 

I should get this instead :

[2021-10-27 23:08:18.897419,....]

Any help?

CodePudding user response:

Try this one:

import datetime
data =[]
dt = datetime.datetime.today()
print(dt)
for i in range(60):
    delta = datetime.timedelta(minutes = i)
    dtnew = dt   delta
    data.append(str(dtnew))
print(data)
  • Related