-1

I am making a table that projects math results into the future. I am adding time stamps on each prediction. If

datetime.datetime.now()

2019-01-07 00:31:15.248895

And If

datetime.datetime.now() + datetime.timedelta(days=14)

2019-01-21 00:31:44.332851

How can I use this logic to turn into a for loop

Because I am asking for the time right now, I can't use range solutions. Like the example here.

I am hopefully expecting a result of 23 different tables each 14 days apart.

mexitaw
  • 119
  • 12
  • 1
    It's really unclear what you're trying to accomplish. Could you edit your question to include more detail of what exactly you are trying to do? – Mattwmaster58 Jan 07 '19 at 05:40
  • Sorry for the confusion. I did edit the question in attempts to make it clearer. I am essentially inserting timestamps in a table that runs multiple math problems. I am only running the script 23 times. The table is predicting every 14 days. – mexitaw Jan 07 '19 at 05:45
  • @mexitaw what's your expected output? – Sociopath Jan 07 '19 at 05:47
  • 23 different tables each with a timestamp of 14 days apart – mexitaw Jan 07 '19 at 05:48

2 Answers2

2

From the question you posted, it is not clear as what your expected output is, but I guess what you want is to get dates in the interval of 14 days. You can simply multiply the values of the loop in days:

Code:

import datetime

for i in range(23):
    print(datetime.datetime.now() + datetime.timedelta(days=14*i))

Output:

2019-01-07 11:17:14.702215
2019-01-21 11:17:14.702332
2019-02-04 11:17:14.702376
2019-02-18 11:17:14.702412
2019-03-04 11:17:14.702448
2019-03-18 11:17:14.702911
2019-04-01 11:17:14.703155
2019-04-15 11:17:14.703187
2019-04-29 11:17:14.703215
2019-05-13 11:17:14.703230
2019-05-27 11:17:14.703417
2019-06-10 11:17:14.703472
2019-06-24 11:17:14.703500
2019-07-08 11:17:14.703514
2019-07-22 11:17:14.703528
2019-08-05 11:17:14.703542
2019-08-19 11:17:14.703556
2019-09-02 11:17:14.703570
2019-09-16 11:17:14.703584
2019-09-30 11:17:14.703598
2019-10-14 11:17:14.703612
2019-10-28 11:17:14.703626
2019-11-11 11:17:14.703640

You can use formatting, if you want only the dates.

Sumit S Chawla
  • 3,180
  • 1
  • 14
  • 33
  • @mexitaw If you are doing other stuff in the `for` loop that is time-consuming, this solution will not work. – iz_ Jan 07 '19 at 05:55
  • @mexitaw : Yes, it depends on the number of loops and the operations performed inside the loop. – Sumit S Chawla Jan 07 '19 at 06:06
  • @Tomothy32 & Sam Thankfully the script is working perfectly! Again thank you so much for the help! :) – mexitaw Jan 07 '19 at 06:09
  • @mexitaw I'm just saying, if the `for` loop takes a long time to execute, this solution could potentially break. – iz_ Jan 07 '19 at 06:11
1
import datetime

for i in range(23):
    timestamp = datetime.datetime.now() + datetime.timedelta(days=14*i)

However, you are going to get a slightly larger difference than 14 days (by a few milliseconds or more, depending on the contents of the for loop) due to the reevaluation of datetime.datetime.now() each iteration. If that's a problem, do this:

now = datetime.datetime.now()
for i in range(23):
    timestamp = now + datetime.timedelta(days=14*i)
iz_
  • 15,923
  • 3
  • 25
  • 40