# Calculating future dates

Discussion in 'Python' started by Toine, Feb 2, 2007.

1. ### ToineGuest

Hello,

I'm new to Python so please bare with me...

I need to calculate a date that is exactly 31 days from the current
date in YYYY-MM-DD format. I know that date.today() returns the
current date, but how can I add 31 days to this result? I'm sure this
task is simple, but I haven't been able to figure it out.

Thanks

Toine, Feb 2, 2007

2. ### Dan BishopGuest

On Feb 1, 6:51 pm, "Toine" <> wrote:
> Hello,
>
> I'm new to Python so please bare with me...
>
> I need to calculate a date that is exactly 31 days from the current
> date in YYYY-MM-DD format. I know that date.today() returns the
> current date, but how can I add 31 days to this result? I'm sure this
> task is simple, but I haven't been able to figure it out.
>
> Thanks

str(datetime.date.today() + datetime.timedelta(31))

Dan Bishop, Feb 2, 2007

3. ### Irmen de JongGuest

Toine wrote:
> Hello,
>
> I'm new to Python so please bare with me...
>
> I need to calculate a date that is exactly 31 days from the current
> date in YYYY-MM-DD format. I know that date.today() returns the
> current date, but how can I add 31 days to this result? I'm sure this
> task is simple, but I haven't been able to figure it out.

>>> import datetime
>>> print datetime.date.today()+datetime.timedelta(days=31)

2007-03-05
>>>

---Irmen

Irmen de Jong, Feb 2, 2007
4. ### ToineGuest

On Feb 1, 4:54 pm, "Dan Bishop" <> wrote:
> On Feb 1, 6:51 pm, "Toine" <> wrote:
>
> > Hello,

>
> > I'm new to Python so please bare with me...

>
> > I need to calculate a date that is exactly 31 days from the current
> > date in YYYY-MM-DD format. I know that date.today() returns the
> > current date, but how can I add 31 days to this result? I'm sure this
> > task is simple, but I haven't been able to figure it out.

>
> > Thanks

>
> str(datetime.date.today() + datetime.timedelta(31))

Your example gave me a few errors but I was able to adapt it into
this:

str(date.today() + timedelta(31))

Toine, Feb 2, 2007
5. ### Ben FinneyGuest

"Toine" <> writes:

> On Feb 1, 4:54 pm, "Dan Bishop" <> wrote:
> > str(datetime.date.today() + datetime.timedelta(31))

>
> Your example gave me a few errors but I was able to adapt it into
> this:
>
> str(date.today() + timedelta(31))

That only works if you're importing 'date' and 'timedelta' into the
current namespace. It's better to keep them in the 'datetime'
namespace, so it's clear what comes from where.

>>> import datetime
>>> str(datetime.date.today() + datetime.timedelta(31))

--
\ "How many people here have telekenetic powers? Raise my hand." |
`\ -- Emo Philips |
_o__) |
Ben Finney

Ben Finney, Feb 2, 2007