-5

How to print ten dates, each two a week apart, starting from today, in the form YYYY-MM-DD

import datetime

now = datetime.datetime.today()

print(now.year)
print(now.month)
print(now.day)
print(now.date())

myDate = now.date()

for i in range(14, 140, 14):
   print(myDate)
DavidG
  • 24,279
  • 14
  • 89
  • 82
  • 1
    Use a `for` loop – Cleptus Apr 16 '18 at 12:55
  • used it , but not sure how to increment the dates, if i inc the date how the month will be incremented and the year as well? – Nitish Attri Apr 16 '18 at 12:56
  • thanks @bradbury9 the follwing code worked. import datetime now = datetime.datetime.today() print(now.year) print(now.month) print(now.day) myDate = now.date() for i in range(1, 140, 14): print(myDate) myDate = myDate + datetime.timedelta(days=i) – Nitish Attri Apr 16 '18 at 13:01

1 Answers1

0
import datetime

dates = []

now = datetime.datetime.today()


for i in range(1, 15):
     day = now.day + i

     date = now.strftime('%Y-%m-' + str(day))

     dates.append(date)

     print(date)

Here is your output:

2018-04-17
2018-04-18
2018-04-19
2018-04-20
2018-04-21
2018-04-22
2018-04-23
2018-04-24
2018-04-25
2018-04-26
2018-04-27
2018-04-28
2018-04-29
2018-04-30

I wasn't sure if you would want to store the dates for some reason, so I also appended them to an empty list if you want to work with the dates later or store them for some reason.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27