0

Is there any function in python that can generate date for example 4 weeks from now or given date?

I've gone through documentation from datetime modeule but couldnt find any example that can support my question.

Anandapadmanathan
  • 315
  • 1
  • 3
  • 8
  • 2
    https://stackoverflow.com/questions/6871016/adding-days-to-a-date-in-python, `datetime.timedelta(weeks=4)` – Shijith Nov 03 '20 at 17:36

4 Answers4

1
four_weeks = datetime.timedelta(days=4*7)
dt = datetime.datetime.now()
print(dt + four_weeks)
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
1

Here you go:

from datetime import timedelta
from datetime import datetime

today = datetime.today()
print(today + timedelta(weeks=1))
Tom J
  • 106
  • 2
  • 14
0

I think the thing you're looking for is timedelta.

from datetime import timedelta

def add_weeks(dt, n_weeks):
    n_days = 7 * n_weeks
    return dt + timedelta(days=n_days)
Adam Kern
  • 566
  • 4
  • 14
0

In python datetime module has a class called datetime which represents a date + time, an point on time line. There is another class called timedelta that represents difference between two dates (datetiems).

You can add a date with a timedelta.

example code:

from datetime import datetime, timedelta

now = datetime.now()
duration = timedelta(days=28)
target = now + duration
print(target)
Farbod Shahinfar
  • 777
  • 6
  • 15